简体   繁体   English

获取选中的单选按钮的值

[英]Get value of a checked radio button

How can I get a value of a checked radio button of a group of related radio buttons without using their ids? 如何在不使用其ID的情况下获取一组相关单选按钮中选中的单选按钮的值? I can try something like this, but it is not generic enough. 我可以尝试类似的方法,但是它不够通用。

var boxes = $('input[name=BankAccountTypeGroup]:checked');
$(boxes).each(function () {
   if ($(this).val() == 'Savings') {
       //
   }
})

Radio buttons are grouped by the name attribute, so your current selector should work fine (without even looping) -- if you have multiple groups of common named groups, you can use the ^= (starts with) inside attribute selector to get all the groups. 单选按钮按name属性分组,因此您当前的选择器应该可以正常工作(没有循环)-如果您有多个公共命名组组,则可以使用属性选择器中的^= (以...开头)获取所有组。

Example, you have multiple radio groups starting with "BankAccount" 例如,您有多个以“ BankAccount”开头的广播组

var groups = $(":radio[name^=BankAccount]:checked").map(function() {
    return this.value;
}).get();

.map() returns a nice array of all your checked values for radio groups starting with "BankAccount" .map()为以“ BankAccount”开头的单选组返回一个很好的数组,其中包含所有检查值

var boxes = $('input[name=BankAccountTypeGroup]:checked');
if (boxes.length==1) //test it, maybe there is no radio checked
    {
        alert(boxes[0].value); //boxes[0] is the first and only-checked element, 
    }

Try this.... 尝试这个....

var $boxes = $('#yourcontaineridOfAllCheckboxes').find('input[type=radio]:checked'));
    $($boxes).each(function () {
       if ($(this).val() == 'Savings') {
           //
       }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM