尝试使用ajax/jquery获取所选单选按钮的值

本文关键字:单选按钮 获取 jquery ajax | 更新日期: 2023-09-27 17:58:47

我正在尝试使用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>

尝试使用ajax/jquery获取所选单选按钮的值

将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()

注意:我还没有测试它