【问题标题】:jquery get value of group of radio buttonsjquery获取单选按钮组的值
【发布时间】:2013-10-14 05:38:40
【问题描述】:

这是我的html代码:

<form name="dform">
<fieldset>
    <input type="radio" class="yes" name="g1" value="1"/> Yes<br/>
    <input type="radio" class="no" name="g1" value="0"/> No
</fieldset>
    <fieldset>
   <input type="radio" class="yes" name="g2" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g2" value="0"/> No
    </fieldset>
    <fieldset>
    <input type="radio" class="yes" name="g3" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g3" value="0"/> No
    </fieldset>
    <input type="reset" class="reset" value="RESET"/><BR/>
</form>
<div id="result">N/A</div>

我想这样设置值:

  1. 如果是得到 3,结果将为“低”
  2. 如果是,得 2 分,结果将是“中等”
  3. 如果没有得到 3 或 2 分,结果将为“高”
  4. 没有检查任何单选按钮将显示“N/A”
  5. 是和否各得 1 分,也会显示“N/A”

我已经尝试过这样的 jquery 脚本:

function scoring(){
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function()){
            score_yes += parseInt ($(this).val());
        });

        $(".no:checked").each(function(){
            score_no += parseInt ($(this).val());
        });

        if($(score_yes)==3){
            $("#result").html('LOW');
        }else if($(score_yes)==2){
            $("#result").html('MEDIUM');
        }else if(($(score_no)==3){
            $("#result").html('HIGH');
        }else if($(score_no)==2){
            $("#result").html('HIGH');
        }else{
            $("#result").html('N/A');
        }
    }
$(document).ready(function(){
    $(".yes:checked").change(function(){
        scoring();
    });
 });

但它没有用。我应该解决什么问题?

【问题讨论】:

  • 什么没用?您是否收到错误消息?
  • @Bartdude 没有任何错误消息,但我所看到的只是结果“N/A”,尽管我点击了任何单选按钮。
  • @AGF,其中至少有两件事会导致控制台错误。

标签: jquery button radio


【解决方案1】:

从几个地方的额外括号到将 int 包装在 jquery 对象中(例如$(score_yes))到绑定.yes:checked(我相信你想要.yes, .no,因为这两个都会改变@ 987654325@)。您还可以进行一些简化。我相信这就是你想要的:

fiddle

function scoring() {
    var score_yes = $(".yes:checked").size();
    var score_no = $(".no:checked").size();

    if (score_yes == 3) {
        $("#result").html('LOW');
    } else if (score_yes == 2) {
        $("#result").html('MEDIUM');
    } else if (score_no == 3) {
        $("#result").html('HIGH');
    } else if (score_no == 2) {
        $("#result").html('HIGH');
    } else {
        $("#result").html('N/A');
    }
}
$(document).ready(function () {
    $(".yes, .no").change(function () {
        scoring();
    });
});

【讨论】:

    【解决方案2】:

    一些语法错误和 score_yes 不应该用 jQuery 包装

    function scoring(){
            var score_yes = 0;
            var score_no = 0;
            $(".yes:checked").each(function(){
                score_yes += parseInt(this.value);
            });
    
            $(".no:checked").each(function(){
                score_no += parseInt(this.value);
            });
    
            if(score_yes==3){
                $("#result").html('LOW');
            }else if(score_yes==2){
                $("#result").html('MEDIUM');
            }else if(score_no==3){
                $("#result").html('HIGH');
            }else if(score_no==2){
                $("#result").html('HIGH');
            }else{
                $("#result").html('N/A');
            }
        }
    $(document).ready(function(){
        $(".yes").change(function(){
            scoring();
        });
     });
    

    DEMO

    【讨论】:

      【解决方案3】:
        function scoring(){
          var score_yes = 0;
          var score_no = 0;
          score_yes = $(".yes:checked").size();
      
          score_no = $(".no:checked").size();
      
         if(score_yes==3){
              $("#result").html('LOW');
          }else if(score_yes==2){
              $("#result").html('MEDIUM');
          }else if(score_no==3){
              $("#result").html('HIGH');
          }else if(score_no==2){
              $("#result").html('HIGH');
          }else{
              $("#result").html('N/A');
          }
      }
      $(document).ready(function(){
         $("#dform input:radio").change(function(){
          scoring();
         });
      });
      

      【讨论】:

        【解决方案4】:

        你可以更简单地做到这一点:

        function scoring() {
            var score_yes = $("input.yes:checked").length;
            var score_no = $("input.no:checked").length;
        
            if (score_no >= 2)
                return 'HIGH';
            else if (score_yes == 3)
                return 'LOW';
            else if (score_yes == 2)
                return 'MEDIUM';
            else
                return 'N/A';
        }
        
        $(document).ready(function(){
            $('input[type="radio"]').change(function(){
                scoring();
            });
        });
        

        【讨论】:

          【解决方案5】:

          我自己的建议是:

          /* caching the radio inputs, to save time later
             (assuming there's a static collection): 
          */
          var radios = $('input[type="radio"]');
          
          // setting up the event-handler:
          radios.change(function(){
              // filtering for how many '.yes' inputs are checked:
              var yes = radios.filter('.yes:checked').length,
              // filtering for how many '.no' inputs are checked:
                  no = radios.filter('.no:checked').length;
          
              // setting the text of the '#result' element:        
              $('#result').text(function(){
                  /* assuming a binary decision, then only, at most,
                     half of the total number of elements can be checked, if they are
                     all choices have been made:
                  */
                  if (yes + no === (radios.length)/2) {
                      /* if 'yes' scores more than 1 (so either 2 or 3) we return 'Low',
                         otherwise 'High':
                      */
                      return yes > 1 ? 'Low' : 'High';
                  }
                  // some are unchecked, therefore we return 'N/A':
                  else {
                      return 'N/A';
                  }
              });
          // triggering the 'change' event on DOM-ready, in case of default-checked radio inputs:
          }).change();
          

          JS Fiddle demo.

          参考资料:

          【讨论】:

            【解决方案6】:

            您的代码中有多个语法错误,其中有多余的 {( 不需要它们。此外,您的主要问题是您的更改侦听器$(".yes:checked") 的选择器仅选择了已选中的单选按钮。您可以使用 $("input[type='radio']") 作为选择器来更改此设置。您可以在下面找到完整的示例

            DEMO

            $(document).ready(function () {
                $("input[type='radio']").change(function () {
                    var score_yes = 0;
                    var score_no = 0;
                    $(".yes:checked").each(function () {
                        score_yes += parseInt($(this).val());
                    });
            
                    $(".no:checked").each(function () {
                        score_no += parseInt($(this).val());
                    });
            
                    if (score_yes === 3) {
                        $("#result").html('LOW');
                    } else if (score_yes === 2) {
                        $("#result").html('MEDIUM');
                    } else if (score_no === 3) {
                        $("#result").html('HIGH');
                    } else if (score_no === 2) {
                        $("#result").html('HIGH');
                    } else {
                        $("#result").html('N/A');
                    }
                    });
                });
            

            【讨论】:

              【解决方案7】:

              多了一个括号:

              $(".yes:checked").each(function() ) {
              

              应该是

              $(".yes:checked").each(function(){
              

              【讨论】:

                猜你喜欢
                • 2016-05-08
                • 1970-01-01
                • 2014-08-01
                • 2012-01-27
                • 2017-02-06
                • 2011-03-28
                • 1970-01-01
                • 2014-04-29
                相关资源
                最近更新 更多