Javascript 当阻止Enter键提交AJAX表单时,关注下一个输入不是';行不通
一个我找不到解决方案的小问题(可能我在谷歌上搜索了错误的关键字): 情况: 我通过按Enter键(13)来防止表单提交。 需要注意的是,表单将由AJAX提交,因此不会重新加载页面。Javascript 当阻止Enter键提交AJAX表单时,关注下一个输入不是';行不通,javascript,jquery,ajax,forms,Javascript,Jquery,Ajax,Forms,一个我找不到解决方案的小问题(可能我在谷歌上搜索了错误的关键字): 情况: 我通过按Enter键(13)来防止表单提交。 需要注意的是,表单将由AJAX提交,因此不会重新加载页面。 $(document).on('keypress', 'input', function (e) { if (e.which === 13) { // Simplifying the selector of the next input // because this isn'
$(document).on('keypress', 'input', function (e) {
if (e.which === 13) {
// Simplifying the selector of the next input
// because this isn't the problem
$(this).next().focus();
return false;
}
});
防止输入键是一项简单的任务,它可以工作:
$(document).on('keypress', 'input', function (e) {
if (e.which === 13) {
return false;
}
});
问题:
由于返回false
,对下一个输入的关注不起作用
如果省略了return false
部分,那么表单自然由AJAX提交,focus()
正在工作。
$(document).on('keypress', 'input', function (e) {
if (e.which === 13) {
// Simplifying the selector of the next input
// because this isn't the problem
$(this).next().focus();
return false;
}
});
关于如何解决这个问题有什么想法吗?尝试在
if
条件中调用函数,并使用$(this).next().focus()代码>在该函数中。在回调
函数中,您可以返回false
以防止表单提交。请提供一些html。它与html无关,表单是正确的。请尝试此操作<代码>$(this).next('input').focus()
;请仔细阅读:如果我省略了return false
部分,那么表单自然是由AJAX提交的,focus()
正在工作。您可以尝试在If条件下调用函数并使用$(this)。next().focus();在这个功能中。在回调函数中,可以返回false以防止表单提交。不确定这是否有效,但值得一试。