如何在textarea关注时输入表单提交? [英] How to submit a form on enter when the textarea has focus?

查看:132
本文介绍了如何在textarea关注时输入表单提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

填写表单的textarea时,回车键被击中时的默认行为是移动到下一行。如何更改表单的行为,以便在用户点击输入时提交,即使用户在textarea中也是如此?

我使用Firebug来检出Stack Overflow的注释textarea(它有这种行为),但看不到任何实现这种效果的JavaScript。有没有办法改变textarea的行为而不使用JavaScript?

Stackoverflow使用 jQuery JavaScript库,它在页面加载时将功能附加到HTML元素上。



以下是您如何使用基本JavaScript来完成的工作:

 < textarea onkeydown =if(event.keyCode == 13){this.form.submit(); return false;}>< / textarea> 

键码13是输入键。



以下是如何使用jQuery来执行此操作,例如Stackoverflow所做的:

 < textarea class =commentarea>< / textarea的> 

with

  $(document).ready(function(){
$('。commentarea')。keydown(function(event){
if(event.keyCode == 13){
this.form.submit();
return false;
}
});
});


When filling out a form's textarea, the default behavior when the enter key is hit is to move to the next line. How can I change the behavior of the form so it will submit upon user hitting enter even when the user is in a textarea?

I used Firebug to checkout Stack Overflow's comment textarea (which has this behaviour), but couldn't see any JavaScript that achieved this affect. Is there a way to change the behavior of the textarea without using JavaScript?

解决方案

You can't do this without JavaScript. Stackoverflow is using the jQuery JavaScript library which attachs functions to HTML elements on page load.

Here's how you could do it with basic JavaScript:

<textarea onkeydown="if (event.keyCode == 13) { this.form.submit(); return false; }"></textarea>

Keycode 13 is the enter key.

Here's how you could do it with jQuery like as Stackoverflow does:

<textarea class="commentarea"></textarea>

with

$(document).ready(function() {
    $('.commentarea').keydown(function(event) {
        if (event.keyCode == 13) {
            this.form.submit();
            return false;
         }
    });
});

这篇关于如何在textarea关注时输入表单提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆