尝试使用ajax/jquery获取选定单选按钮的值 [英] Trying to get value of selected radio button using ajax/jquery

查看:469
本文介绍了尝试使用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()

注意:我尚未测试

这篇关于尝试使用ajax/jquery获取选定单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆