尝试使用ajax/jquery获取选定单选按钮的值
问题描述:
我试图获取使用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. 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>
答
将HTML更改为,这里我在单选按钮中添加了通用类spacingCheckBox
Change Your HTML as, Here I have added common class spacingCheckBox
to radio buttons
<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()
注意:我尚未测试