【发布时间】:2011-09-27 16:43:15
【问题描述】:
我已禁用我的 enter 键,以便我可以进行 ajax 提交,但我想确保如果用户在服务器的响应返回之前按两次 enter 不会提交两次表单。
这里我禁用了回车键并为其分配了一个名为submitForm()的函数:
$(document).ready(function(){
offset = $("#quantity").html();
$("#lastName").focus();
$(document).bind("keypress" , function(e){
if (e.which == 13 && (!$("#notes").is(":focus")) && (!$("#submit").is(":focus"))) {
submitForm(); return false;
}
})
$("#submit").click(function(){ submitForm(); });
});
我尝试在其他地方再次bind(),但它没有做任何事情。我在不同的地方尝试了preventDefault(),但我认为我要么把它放在错误的地方,要么这是错误的做法。我似乎无法让它工作。
我找到了这个:How to disable Enter/Return Key After a function is executed because of it?,但它没有回答我的问题,因为在发布者的示例中,脚本会检查框中是否有任何内容,但我想查看我是否已提交或不是。我不知道该怎么做。我真正想做的是禁用回车键,直到我收到来自服务器的 ajax 调用回复。这里是 submitForm():
function submitForm() {
$.post("/ajax/files" , $("#files").serialize(), function(data){
filesReset();
if (data != "") {
$("#lastInsert table tbody:last").prepend(data);
$("#lastInsert table tbody:last tr:first").find("td").hide();
$("#lastInsert table tbody:last tr:first").find("td").fadeIn(1000);
$("#lastInsert table tbody:last tr:first").effect("highlight" , {"color": "aqua"}, 1000);
$("#lastInsert table tbody:last tr:last").remove();
} else {
alert("Insert rejected: either insufficient criteria or the server is down.");
$("#hidden").click();
}
});
}
我不想在服务器端做任何事情,因为我需要这个提交功能尽可能快(这将是一个快节奏的数据输入表单)。
【问题讨论】:
标签: jquery ajax double-submit-prevention