Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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
Javascript 当阻止Enter键提交AJAX表单时,关注下一个输入不是';行不通_Javascript_Jquery_Ajax_Forms - Fatal编程技术网

Javascript 当阻止Enter键提交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'

一个我找不到解决方案的小问题(可能我在谷歌上搜索了错误的关键字):

情况:

我通过按Enter键(13)来防止表单提交。 需要注意的是,表单将由AJAX提交,因此不会重新加载页面。

$(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以防止表单提交。不确定这是否有效,但值得一试。