简体   繁体   English

输入键以使用php id乘以表单的情况下使用javascript提交表单

[英]Enter key to submit form with javascript in case of multiply form with php id

I have separate reply form with each post with it's own php $id in same page. 我在每个页面上都有单独的回复表单,每个帖子都有自己的php $id So I want to submit each reply with keyboard Enter key. 因此,我想使用键盘Enter键提交每个答复。 I collect this javascript code below from here, but I cannot understand how apply it for each reply. 我从下面收集了以下JavaScript代码,但无法理解如何将其应用于每次回复。

BN: Collected code not working also. BN:收集的代码也不起作用。

my form: 我的表格:

<form action="" method="post" class="repfrm'.$id.'" id="prepfrm">
<textarea name="replycom" id="replycom'.$id.'" class="replycom"></textarea>
<button type="submit" name="submit" id="'.$id.'" class="replyfrm">Post Reply</button>
</form>

Collected Script From stockoverflow: 从库存溢出中收集的脚本:

$(".replycom").keyup(function(event){
   if(event.keyCode == 13){
       $(".replyfrm").click();
   }
});

Something like that: 像这样:

Bind keypress to textarea and submit the closest form when press the key 将按键绑定到textarea并在按键时提交最接近的表单

$("textarea").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $(this).closest("form").submit();
    }
});

Try this script 试试这个脚本

 $(".replycom").keypress(function (e) {
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
        }

    });

Change textarea to textbox and you press enter in textBox then it works which you want. textarea更改为textbox ,然后在textBox中按Enter,然后按需要运行即可。 Do not required jQuery code. 不需要jQuery代码。

<form action="" method="post" class="repfrm'.$id.'" id="prepfrm">
   <input type="text" name="replycom" id="replycom'.$id.'" class="replycom">
   <button type="submit" name="submit" id="'.$id.'" class="replyfrm">Post Reply</button>
</form>

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

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