简体   繁体   English

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

[英]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.

相关问题 如何使用jQuery获取或设置单选按钮列表的选定索引? - How to get or set Selected index of the Radio Button List using jquery? 如何使用jquery获取选定的asp单选按钮? - how can i get the selected asp radio button using jquery? 使用jQuery获取Ajax:ComboBox的选定值 - Get the selected value of Ajax:ComboBox using jQuery 使用javascript循环通过asp.net单选按钮列表以获取选定的值和文本 - using javascript to loop through asp.net radio button list to get selected value and text 无法使用MVC在自定义验证中检查单选按钮选择的值 - Not able to check the radio button selected value in custom validation using mvc 如何使用MVVM模式获取组中的选定单选按钮? - How to get selected radio button in group using MVVM pattern? 使用FindControl通过GroupName获取选定的单选按钮 - Using FindControl to get selected radio button via GroupName 获取单选按钮值 - Get radio button value 如何在不使用GroupBox的情况下为单选按钮提供值并确定选定的单选按钮 - How can I give a radio button a value and determine the selected radio button without using a GroupBox 使用jQuery Ajax Post填充下拉列表,并在C#页面上获取选定的值 - Fill drop downlist using jquery ajax post and get selected value on c# page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM