Reputation: 49
This is my html code.
<form id="item">
<div class="form-group col-lg-3 col-md-3 col-sm-3 col-sx-3">
<label for="itemtype">Hotel</label>
<input type="radio" class="radio" id="itemtype" name="optradio" value="H" checked>
<label for="itemtype">AirPort</label>
<input type="radio" class="radio" id="itemtype" name="optradio" value="A">
</div>
</form>
And this is my jquery.
function save() {
debugger
$.ajax({
type: "POST",
data: { "item": { "itemid": $("#itemid").val(), "itemtype": $('#itemtype').val() }},
url: "@Url.Action("Save", "Item")",
cache:false,
success: function (data) {
debugger
if (data.isSuccess) {
alert("Saved Succesfully!");
window.location.href = "@Url.Action("Index", "Item")";
}
}, error: function (err) {
alert("Saved Failed!")
debugger
}
});
}
All the time when I trying to save the item type call "A", it never happen. Always this query getting radio button value as "H". I need to pass the 2nd value "A".
Upvotes: 0
Views: 141
Reputation: 305
$('input[name=optradio]:checked').val();
it is not recommended to have two identical ids on one page
other solution using class name:
<form id="item">
<div class="form-group col-lg-3 col-md-3 col-sm-3 col-sx-3">
<label for="itemtype">Hotel</label>
<input type="radio" class="radio itemtype" name="optradio" value="H" checked>
<label for="itemtype">AirPort</label>
<input type="radio" class="radio "itemtype" name="optradio" value="A">
</div>
</form>
$('.itemtype:checked').val();
In jquery the best way to send data form a formular is to use serialize() or serializeArray() methods.
Some exemple : serialize-form-data-to-json
Upvotes: 0
Reputation: 1059
You can get the correct value by using the name, please remove the duplicate id
$('input[name=optradio]:checked', '#item').val()
Upvotes: 2