gpt4 book ai didi

jquery - 防止 jquery 在特定字段上按下回车键时提交

转载 作者:行者123 更新时间:2023-12-01 04:22:05 25 4
gpt4 key购买 nike

当我在特定输入字段上按 Enter 键时,如何取消 jquery 提交的运行?

类似这样的事情:

var $canSubmit = true;
$('#profile_form')
.keyup(function(e){
if (e.keyCode == 13 && e.srcElement.name == 'countrySearch') {
$canSubmit = false;
} else
$canSubmit = true;
})
.on('submit', function(e){
e.preventDefault();
console.log(e);

if (!$canSubmit) return false;

// first time enter is clicked when on countrySearch this is run as submit runs before keyup
// i want all code here not to run when enter is clicked on input[name="countrySearch"]
});

最佳答案

您应该使用 keypress 而不是 keyup 和 e.target.name

var $canSubmit = true;
$('#profile_form').keypress(function(e) {
if (e.keyCode == 13 && e.target.name == 'countrySearch') {
$canSubmit = false;
} else {
$canSubmit = true;
}
}).on('submit', function(e) {
if (!$canSubmit) {
return false;
} else {
alert('this would submit');
}

});

在这里摆弄http://jsfiddle.net/tazcu/1/

关于jquery - 防止 jquery 在特定字段上按下回车键时提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9566936/

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