gpt4 book ai didi

javascript - Enter key to send 在 Javascript 中只工作一次

转载 作者:行者123 更新时间:2023-11-29 19:03:43 25 4
gpt4 key购买 nike

我有一个提交表单的代码,每当我们在文本区域按 enter 键时......它有效,但代码只运行一次。每当我再次按回车键提交时,它都不起作用。

这是我的 Javascript 代码:

$('.comment_ta').on("keypress" , function(e){

if (e.which == 13) {
$(this).closest('form').submit();
}

});

这里是表单和文本区域..代码在 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'

现在它第一次运行良好,但每当我在提交后呈现新表单时它都不起作用。

最佳答案

试一试:

$(document).on('keypress', '.comment_ta', function(e){

if (e.which == 13) {
$(this).closest('form').submit();
}

});

我假设这是因为 .comment_ta 元素在 DOM 中被替换,这意味着 .on 事件监听器将不再工作。通过将事件监听器应用于 document,事件监听器应该继续工作。

关于javascript - Enter key to send 在 Javascript 中只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com