Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按Enter键提交文本区域表单,不带提交按钮_Php_Javascript_Forms_Submit_Enter - Fatal编程技术网

Php 按Enter键提交文本区域表单,不带提交按钮

Php 按Enter键提交文本区域表单,不带提交按钮,php,javascript,forms,submit,enter,Php,Javascript,Forms,Submit,Enter,背景: 我正在制作一个类似facebook墙的页面,它将有许多帖子,你应该能够评论每一篇帖子。因此,在这一页中有许多形式(当然)。我只需要提交其中一份 是的,我已经找到了这个问题的答案,但没有一个有效,所以在这里提问: 我有一张这样的表格: <form enctype="text/plain" action="submitcomment.php" method="post" target="output_frame" id="comment<?php echo $prepar

背景: 我正在制作一个类似facebook墙的页面,它将有许多帖子,你应该能够评论每一篇帖子。因此,在这一页中有许多形式(当然)。我只需要提交其中一份

是的,我已经找到了这个问题的答案,但没有一个有效,所以在这里提问:

我有一张这样的表格:

    <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>
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;
}

如果我使用文本框,它会工作,但当我使用textarea时,它就不工作了。尝试按enter键不起任何作用。

使用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');
        }
    });
});

不过要小心,它会在每一行上提交。人们倾向于在textareas中编写多行文本,因此这种行为可能是意外的

像以前一样,将textareas嵌套在表单中,但为它们指定类名而不是id。这样,jquery就可以引用DOM中具有该特定类名的所有TextArea

<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>

这个代码流在一个应用程序中运行良好。请注意,您不应使用
e.charCode
,因为
e.keyCode
(IE)和
e.
(其他所有内容)就足够了。看

此外,查找表单的代码过于复杂且不必要,请更改以下内容:

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

target.form.submit();

在一个环境中工作很好。您不应该使用
charCode
属性,请参阅。可能是“document.forms[this.form.id].submit();”部分有问题?因为它至少对我不起作用:/b你可以用
target.form.submit()
替换所有这些,b你应该接受其中一个答案。如果他们都没有帮助,发布你自己的答案,以及你为解决这个问题所做的努力,并接受你自己的答案。你的评论解决了这个问题,我如何将其作为答案应用P我也不能在8小时内发布我自己的答案,因为我没有足够的声誉!这听起来不错。但是每个表单ID都是不同的。如何让jQuery知道要提交哪一个?我对JavaScript/AJAX/jQuery完全是新手。。。到目前为止,你已经用PHP完成了所有的工作:)你必须用唯一的ID标记你的表单/文本区域,并且你可以像对表单ID那样从PHP中回显一些内容。我修改了上面的代码,使其不绑定到文本区域ID,而是绑定到通用标记。更好的做法是将某些类分配给所有textarea,并将其用作选择器,而不是“form>textarea”,以避免与其他表单重叠,这些表单您不想增强Touch,在重写之前没有注意到您对我的代码的更新,但完全赞同您的修改+1我们能用这种方法检测组合吗?例如Shift+Enter添加类似于Facebook的新行。@SuryaS
target.form.submit();