[英]How to get or set Selected index of the Radio Button List using jquery?
[英]Trying to get value of selected radio button using ajax/jquery
我試圖獲取使用ajax和jquery選擇的任何單選按鈕的值,但我只能執行以下操作,當然只有選中或不選擇單選按鈕才能使用。 我究竟做錯了什么?
$('#urgencyJList').change(function () {
var modelData = {
paperType: $("#paperTypeJList").val(),
urgency: $("#urgencyJList").val(),
numOfPages: $("#numOfPagesJList").val(),
**spacing: $("#doubleSpacingV").val()**
};
$.ajax({
url: '@Url.Action("getNewPrice","Home")',
data: modelData,
type: "POST",
cache: false,
async: true,
success: function (response) {
document.getElementById('priceLabel').innerHTML = response;
}
});
});
我的單選按鈕代碼如下:
<div class="row">
@Html.LabelFor(model => model.spacing, "Spacing:")
<fieldset>
<label for="doubleSpacingV">
Double Spacing
@Html.RadioButtonFor(model => model.spacing, "double", new { id = "doubleSpacingV", @checked = true })
</label>
<label for="singleSpacingV">
Single Spacing
@Html.RadioButtonFor(model => model.spacing, "single", new { id = "singleSpacingV" })
</label>
@Html.ValidationMessageFor(model => model.spacing)
</fieldset>
</div>
將您的HTML更改為,此處我已將常用類spacingCheckBox
添加到單選按鈕
<fieldset>
<label for="doubleSpacingV">
Double Spacing
@Html.RadioButtonFor(model => model.spacing, "double", new { @class = "spacingCheckBox", @checked = true })
</label>
<label for="singleSpacingV">
Single Spacing
@Html.RadioButtonFor(model => model.spacing, "single", new { @class = "spacingCheckBox" })
</label>
@Html.ValidationMessageFor(model => model.spacing)
</fieldset>
要檢索已選中的復選框值,請使
spacing: $('.spacingCheckBox:checked').val()
注意:我還沒有測試過
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.