jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 按Enter键检查或选择复选框大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
新手 – 我想用这些复选框做两件事:

>使用TAB键选项卡,这部分有效
>当我选择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>

解决方法

试试这个代码
<!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>

大佬总结

以上是大佬教程为你收集整理的jquery – 按Enter键检查或选择复选框全部内容,希望文章能够帮你解决jquery – 按Enter键检查或选择复选框所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。