[英]Enter key to send works only once in Javascript
I have a code that submits the form, whenever we press enter key on text area... it works, but code runs only once.我有一个提交表单的代码,每当我们在文本区域按下回车键时......它可以工作,但代码只运行一次。 Whenever I press enter again to submit it doesn't work.
每当我再次按 Enter 提交时,它都不起作用。
Here is my Javascript code:这是我的 Javascript 代码:
$('.comment_ta').on("keypress" , function(e){
if (e.which == 13) {
$(this).closest('form').submit();
}
});
and here is the form and text area .. code is in ruby n haml这是表单和文本区域 .. 代码在 ruby n haml 中
=form_for( ([c,c.confessioncomments.build]), remote: true ) do |f|
.cmnt
=f.text_area :content class: "form-control comment_ta"
=f.submit 'Comment' , clasS:'btn btn-default cbtn'
Now it works well for the first time but whenever I render the new form after submission it doesn't work.现在它第一次运行良好,但是每当我在提交后呈现新表单时它都不起作用。
Give this a shot:试一试:
$(document).on('keypress', '.comment_ta', function(e){
if (e.which == 13) {
$(this).closest('form').submit();
}
});
I assume this is because the .comment_ta
element gets replaced in the DOM, which means the .on
event listener will no longer work.我认为这是因为
.comment_ta
元素在 DOM 中被替换,这意味着.on
事件侦听器将不再工作。 By applying the event listener to the document
instead, the event listener should continue to work.通过将事件侦听器应用于
document
,事件侦听器应该继续工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.