获取已检查单选按钮的值

时间:2014-02-13 15:42:40

标签: javascript jquery

如何在不使用其ID的情况下获取一组相关单选按钮的已检查单选按钮的值?我可以尝试这样的东西,但它不够通用。

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

3 个答案:

答案 0 :(得分:0)

单选按钮按name属性分组,因此您当前的选择器应该可以正常工作(甚至没有循环) - 如果您有多组常见命名组,则可以使用^=(以内部属性选择器开始,以获取所有组。

例如,您有多个以“BankAccount”

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

.map()为以“BankAccount”开头的广播组的所有已检查值返回一个很好的数组

答案 1 :(得分:0)

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, 
    }

答案 2 :(得分:0)

试试这个......

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