【问题标题】:How to detect checked status of radio buttons in jquery?如何检测 jquery 中单选按钮的选中状态?
【发布时间】:2016-09-15 13:21:04
【问题描述】:

我有一组单选按钮,第一个是在呈现 html 时静态设置的。其余的是使用 jQuery 动态创建的。下面给出了创建单选按钮的代码

for (var i = 1; i < length; i++){
        $('.radio').append('<br>');
        $('.radio').append('<label><input type = "radio" id = '+options[i]+' value = '+i+' name = "options" class = "radio-options"/><p>'+options[i]+'</p></label>');
    }

    //creating the last radio button here
    $('.radio').append('<br>');
    $('.radio').append('<label><input type = "radio" id = "Custom" value = '+length+' name = "options" class = "radio-options"/><p>Custom</p></label>')

我有一个提交按钮,当我单击它时,我想检查哪个单选按钮具有选中的属性属性。这是我的尝试。

$('.submit-butt').on('click', function(){

在调用提交按钮单击处理程序后,我立即打印所有单选按钮的选中属性状态。无论我是否选择了单选按钮,它们都被证明是错误的。

                    for(var i = 0; i < options_length; i++){
                        console.log($('input:radio[name=options]').prop('checked'));
                    }


                   if($('input:radio[name=options]').prop('checked', true)){
                       console.log('Found my value');

                       var _this = $('input:radio[name=options]:checked');

但是,由于某种原因,此控制台日志语句将 Custom 作为 id 打印出来,这是最后一个单选按钮的 id,即使我没有选择它。

console.log(_this.attr('id'));

这是我第二次打印所有单选按钮的选中属性的地方。它在这里也被证明是错误的,包括在上一行中打印出来的 Custom id 单选按钮。

                       for(var i = 0; i < options_length; i++){
                        console.log($('input:radio[name=options]').prop('checked'));
                    }
                   }
                });

无论我是否选择单选按钮,也无论是哪个单选按钮,当我点击提交按钮时,会立即选择自定义单选按钮,即使该单选按钮的选中属性仍会打印到错误的。我该如何解决这个问题?

【问题讨论】:

  • O'boy,首先options_length 是什么?其次,if ( $(element).prop('checked', true) )... 始终为真,并且不检查是否检查了某些内容,它设置选中的属性为真并返回一个 jQuery 集合。
  • $('input:radio[name=options]').prop('checked', true) sets 不检查是否检查了你想要$('input:radio[name=options]').is(':checked')$('input:radio[name=options]').prop('checked') == true
  • @adeneo options_length 基本上是一个变量,它检测用户输入了多少个选项,基于该变量我创建了复选框的数量。
  • @Liam - 所以我尝试了你刚才所说的。它似乎只适用于第一个静态创建的单选按钮。如何让它为动态创建的单选按钮工作?

标签: javascript jquery html button


【解决方案1】:

您应该为动态创建的单选按钮的 id 和其他属性添加引号:

for (var i = 1; i < length; i++){
        $('.radio').append('<br>');
        $('.radio').append('<label><input type = "radio" id = "'+options[i]+'" value = "'+i+'" name = "options" class = "radio-options"/><p>'+options[i]+'</p></label>');
    }

    //creating the last radio button here
    $('.radio').append('<br>');
    $('.radio').append('<label><input type = "radio" id = "Custom" value = "'+length+'" name = "options" class = "radio-options"/><p>Custom</p></label>')

【讨论】:

  • 这能解决我的问题,还是仅仅与良好的编码习惯有关?
  • @ZaidHumayun - 这是修复的一半。引号不是可选的。你需要它们。
【解决方案2】:

不要忘记引号..

$('.radio').append('<label><input type = "radio" id = "'+options[i]+'" value = "'+i+'" name = "options" class = "radio-options"/><p>'+options[i]+'</p></label>');

试试这样的。

for(var i = 0; i < options_length; i++){
    console.log($('#'+options[i]).is(':checked'));
}

【讨论】:

  • 所以我添加了引号并尝试了您的 if 语句版本,使用 .is(':checked'),但没有 for 循环,它似乎检测到我是否选择了收音机现在按钮。现在,如何在不通过 for 循环运行的情况下检测选择了哪个单选按钮?
  • 哦,我明白你为什么要使用名称。使用$("input[name='options']").is(":checked")@ZaidHumayun
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2011-04-28
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 2011-04-15
  • 2011-07-29
相关资源
最近更新 更多