[英]Trying to get value of selected radio button using ajax/jquery
I'm trying to get the value of whichever radio button is selected using ajax and jquery but I have only been able to do the following and of course it only works if that radio button is selected or not. 我试图获取使用ajax和jquery选择的任何单选按钮的值,但我只能执行以下操作,当然只有选中或不选择单选按钮才能使用。 What am I doing wrong? 我究竟做错了什么?
$('#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;
}
});
});
My code for the radio buttons is below: 我的单选按钮代码如下:
<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>
Change Your HTML as, Here I have added common class spacingCheckBox
to radio buttons 将您的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>
To retrieve checked checkbox value use 要检索已选中的复选框值,请使
spacing: $('.spacingCheckBox:checked').val()
Note: I have not tested it 注意:我还没有测试过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.