Reputation: 663
Can we post a form with a dropdown value through ajax as document.getElementById(dropdown).value as the selected value of drop down , Or is it neccessary that we should use selected.Index
Upvotes: 0
Views: 394
Reputation: 7778
I use this JS function to make sure that all browsers getting the value:
function getDataForDropdown(colName){
var columnName = "";
if ( columnName == null || columnName == '' )
{
var e = document.getElementById(colName);
columnName = e.options[e.selectedIndex].value;
}
if ( columnName == null || columnName == '' || columnName.length==0)
{
columnName = document.getElementById(colName).value;
}
if ( columnName == null || columnName == '' || columnName.length==0)
{
var select = document.getElementById(colName);
columnName= select.options[select.selectedIndex].value;
if ( columnName == null || columnName == '' || columnName.length==0)
{
columnName= select.options[select.selectedIndex].text;
}
}
return columnName;
}
Upvotes: 0
Reputation: 147403
The short answer is "yes", but make sure that all options have a value attribute or property first, as not all browsers correctly report the option value where no value has been set (i.e. instead of sending the content (text), they send nothing). That goes for normal form submission too.
Upvotes: 2
Reputation: 4419
It makes no real difference.
I use .value
if I have to, but generally I'll use jQuery and thus have the .val()
function (or .value()
in prototype)
Upvotes: 1
Reputation: 478
Yes it is possible. You can use jquery.ajax, create an object with all the values (fields on form) you want to post and pass it to data: and set the type:"POST".
var valuestoPost = {field1:"val1", field2:"val2", dropdownval:$("#dropdownid").val()};
$.ajax({
url: foopostURL,
dataType: "html",
type: 'POST',
data: valuestoPost,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success:function (data){
//operation on successful post
},
error: function(jqXHR, textStatus, errorThrown){
//operation on error
}
});
Upvotes: 0