简体   繁体   English

提交textarea表单,而没有带有Enter键的提交按钮

[英]Submit textarea form without a submit button with Enter key press

Background: I'm making a facebook wall-alike page, which will have many posts and you should be able to comment every post. 背景:我正在制作一个类似Facebook墙的页面,其中将包含许多帖子,您应该能够对每个帖子发表评论。 So in this one page there are many forms (of course). 因此,在这一页中,有很多形式(当然)。 And I only need to submit one of them. 我只需要提交其中一个即可。

So yes, I have found answers to this question , but none of them work, so asking here: 因此,是的,我找到了该问题的答案,但是它们都无效,因此请在此处询问:

I got a form like this: 我得到这样的表格:

    <form enctype="text/plain" action="submitcomment.php" method="post" target="output_frame" id="comment<?php echo $prepare_data['post_id']; ?>">
    <textarea name="comment" id="comment" onkeypress="return submitViaEnter(event)" value="" autocomplete="off"></textarea>
    <input type="hidden" name="hiddenid" id="hiddenid" value="<?php echo $prepare_data['post_id']; ?>" />
    </form>

and my JavaScript function looks like this: 我的JavaScript函数如下所示:

function submitViaEnter(evt) {
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var form = target.form;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {
        document.forms[this.form.id].submit();
        alert("sent!");
        return false;
    }
    return true;
}

If I use a textbox, it works, but when I use textarea, it is not working. 如果使用文本框,则可以使用,但是使用textarea时,则不能使用。 Trying to press enter does nothing. 尝试按Enter键无济于事。

Use jQuery and bind function to event via javascript: 使用jQuery和绑定功能,通过JavaScript事件:

$(function(){
    $('form > textarea').on('keyup', function(e){
        if (e.keyCode == 13) {
            // do whatever you want to do, for example submit form
            $(this).parent('form').trigger('submit');
        }
    });
});

Be careful with this though - it will submit on every new line. 不过要小心-它会在每个新行中提交。 People tend to write multiline texts in textareas so this behavior could be unexpected 人们倾向于在文本区域中编写多行文本,因此这种行为可能是意外的

This code flow works fine in a jsfiddle test . 此代码流工作在一个精致的jsfiddle测试 Note that you should not be using e.charCode , as e.keyCode (IE) and e.which (everything else) suffice. 请注意,你不应该使用e.charCode ,作为e.keyCode (IE)和e.which (一切)就足够了。 See this question . 看到这个问题

Also, your code for finding the form is overly complex and not necessary, change this: 此外,您查找窗体的代码是过于复杂,没有必要,更改此:

var form = target.form;
...
document.forms[this.form.id].submit();

To this: 对此:

target.form.submit();

Have your textareas nested within your forms, as before, but give them classnames instead of id's. 与以前一样,将文本区域嵌套在表单中,但给它们类名而不是id。 That way, the jquery can reference all textareas in the DOM with that particular classname. 这样,jQuery可以使用特定的类名引用DOM中的所有文本区域。

<form name="form1" enctype="text/plain" action="submitcomment1.php" method="post" target="output_frame">
<textarea name="comment1" class="comment" value="" autocomplete="off"></textarea>
</form>
<form name="form2" enctype="text/plain" action="submitcomment2.php" method="post" target="output_frame">
<textarea name="comment2" class="comment" value="" autocomplete="off"></textarea>
</form>

Then, adjust Jura's code like so and it should refer to the correct form that the textfield is living in. 然后,像这样调整Jura的代码,它应该引用文本字段所处的正确形式。

$(function(){

  $('.comment').on('keyup', function(e){

    if (e.keyCode == 13) {

      $(this).parent('form').trigger('submit');

    }
  });
});

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

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