【问题标题】:Hit Enter key to Check or select checkbox按 Enter 键检查或选择复选框
【发布时间】:2012-09-06 13:04:05
【问题描述】:

新手-我想用这些复选框做两件事:

  1. 使用 TAB 键来切换选项,这部分有效
  2. 当我通过选项 TAB 时,我想按 ENTER 键来选中该复选框,这部分不起作用

以下是示例代码。我将复选框作为一个组使用。

大家有什么建议吗?

<!doctype html>
    <head>
        <title>test Radio buttons checkbox</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('input:checkbox[name=Colors]').keypress(function(event) {
                    var keycode = (event.keyCode ? event.keyCode : event.which);
                    if (keycode == 13) {
                        Checkbox_to_RadioButton(this);
                        alert("Enter key was pressed");
                    }   
                    event.stopPropagation();
                });

                $('input:checkbox[name=Colors]').click(function(){
                    Checkbox_to_RadioButton(this);
                });
            });

            function Checkbox_to_RadioButton(box){
                $('input:checkbox[name=' + box.name + ']').each(function(){
                    if (this != box)
                        $(this).attr('checked', false);
                });
            }
        </script>
    </head>

    <body>
        <h1>test Radio buttons checkbox</h1>
        <form name="form1">
            <input type="text" id="dname" name="dname"><br>
            <input type="checkbox" id="Colors" name="Colors" value="Red" />Red<br />
            <input type="checkbox"  id="Colors" name="Colors" value="Blue" />Blue<br />
            <input type="checkbox" id="Colors"  name="Colors" value="Green" />Green<br     />
            <input type="checkbox" id="Colors"  name="Colors" value="Yellow"         />Yellow<br /> 
            <br>
        </form>
    </body>
</html>

【问题讨论】:

  • 按空格不是一个选项?如果没有,请尝试使用keydown 事件而不是keypress
  • 请注意,可以使用空格选择复选框。 stackoverflow.com/a/35423812/1343917

标签: jquery forms checkbox enter


【解决方案1】:

首先,form 标记前缺少一个括号。

其次,您忘记实际激活按键上的复选框:

if (keycode == 13) {
    $(this).attr('checked', checked); // << this line!
    Checkbox_to_RadioButton(this);
    alert("Enter key was pressed");
}   

Working Fiddle here.

【讨论】:

    【解决方案2】:

    试试这个代码

    <!doctype html>
    <html>
      <head>
        <title>test Radio buttons checkbox</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('input:checkbox[name=Colors]').keypress(function(event) {
                    var keycode = (event.keyCode ? event.keyCode : event.which);
                    if (keycode == 13) {
                        Checkbox_to_RadioButton(this,"enter");
                    }   
                    event.stopPropagation();
                });
    
                $('input:checkbox[name=Colors]').click(function(){
                    Checkbox_to_RadioButton(this,"click");
                });
            });
    
            function Checkbox_to_RadioButton(box,myEvent){
                if(myEvent == "enter")
                {
                    var $box = $(box);
                    if($box.attr('checked'))
                        $box.attr('checked',false);
                    else
                        $box.attr('checked',true);
                }
                $('input:checkbox[name=' + box.name + ']').each(function(){
                    if (this != box)
                        $(this).attr('checked', false);
                });
            }
        </script>
    </head>
    
    <body>
        <h1>test Radio buttons checkbox</h1>
        <form name="form1">
            <input type="text" id="dname" name="dname"><br>
            <input type="checkbox" id="Colors" name="Colors" value="Red" />Red<br />
            <input type="checkbox"  id="Colors" name="Colors" value="Blue" />Blue<br />
            <input type="checkbox" id="Colors"  name="Colors" value="Green" />Green<br     />
            <input type="checkbox" id="Colors"  name="Colors" value="Yellow"         />Yellow<br /> 
            <br>
        </form>
      </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      我发现推荐的解决方案过于臃肿。这样效果更好

      $('input:checkbox').keypress(function(e){
          if((e.keyCode ? e.keyCode : e.which) == 13){
              $(this).trigger('click');
          }
      });
      

      【讨论】:

        【解决方案4】:

        r3wt 的方法效果很好,但我注意到在我的表单上按Enter 也会提交表单或做其他不需要的事情。添加e.preventDefault(); 可防止在复选框上按回车时默认浏览器操作。

        $('input:checkbox').keypress(function(e){
            e.preventDefault();
            if((e.keyCode ? e.keyCode : e.which) == 13){
                $(this).trigger('click');
            }
        });
        

        【讨论】:

          【解决方案5】:

          这对我有用。

          $('input:checkbox').keypress(function(e){
              if((e.keyCode ? e.keyCode : e.which) == 13){
                  $(this).trigger('click');
              }
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-01
            • 2013-06-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多