简体   繁体   English

单击“输入”按钮而不进行任何更改

[英]Clicking on Enter button without making any changes

I'm creating a form with multiple labels, and I used this javascript so I can click on the Enter button (in the keyboard) without jumping into the submit button (in the form), the following is working well but just in 1 label, how can I using it for all the labels without copy & past it changing the label id? 我正在创建一个带有多个标签的表单,并且使用了这个javascript,因此无需单击“ 提交”按钮(在表单中),就可以单击“ 输入”按钮(在键盘中),以下代码工作正常,但仅使用1个标签,如何在不复制的情况下其用于所有标签更改标签ID?

<script>
    $(document).ready(function() {
        $('#text1').keydown(function(event){
            if(event.keyCode == 13) {
                event.preventDefault();
                document.getElementById("text1").value = document.getElementById("text1").value + "\n";
                return false;
            }
        });

        $('#text2').keydown(function(event){
            if(event.keyCode == 13) {
                event.preventDefault();
                document.getElementById("text2").value = document.getElementById("text2").value + "\n";
                return false;
            }
        });
    });

    ...
    ...

</script>

Since you are binding your key listener to $('#text1') and $('#text2') , it will only listen when your key is pressed while focusing these elements. 由于您将键侦听器绑定到$('#text1')$('#text2') ,因此仅在按下这些键并集中这些元素时,它才会侦听。

You have to bind your listener to a different selector that catches all your inputs, eg.: 您必须将侦听器绑定到另一个捕获所有输入的选择器,例如:

$('.form-class input').keydown(function(event){
    if(event.keyCode == 13) {
        event.preventDefault();
         ...
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM