简体   繁体   English

Keydown事件Javascript?

[英]Keydown event Javascript?

I wrote a script that trigger a submit button when i press enter key in a textarea, the TextArea returns to the line before the submit is activated. 我编写了一个脚本,当我在textarea中按Enter键时会触发一个提交按钮,TextArea返回到激活提交之前的行。

I want to obstruct the TextArea to not return to the line 我想阻止TextArea不返回该行

there is my script 有我的剧本

jQuery( "#div1" ).on( "keydown", function(event) {
    if(event.which == 13){ 
        if(document.getElementById('checkbox').checked){             
            jQuery("#envoyer").trigger('click');
        }
    }
});

You need to cancel the event! 您需要取消活动!

event.preventDefault();

Just make sure to only do that when you're submitting the form - it'd be very easy to accidentally nuke all ability to type in the textarea at all ;) 只要确保仅在提交表单时这样做即可,很容易不小心轻敲所有可以在textarea中键入的功能;)

Also, as a usability hint, I would suggest adding && !event.ctrlKey && !event.shiftKey to your if condition, as this allows people to enter line breaks manually as they would in many chat programs - if you explicitly don't want newlines, you may be better just using <input type="text" /> instead of <textarea> , especially since this comes with built-in Enter-to-submit-form functionality. 另外,作为可用性的提示,我建议在您的if条件中添加&& !event.ctrlKey && !event.shiftKey ,因为这允许人们像在许多聊天程序中一样手动输入换行符-如果您明确不希望这样做换行符,最好使用<input type="text" />而不是<textarea> ,尤其是因为它带有内置的“提交表单”功能。

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

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