我的问题是,在单击任何表单提交按钮后,使用 jQuery 实现禁用页面按钮的最佳方法是什么。 类似的东西可以在这里 Is js executed after form synchronized submit其中指出我的问题代码 (1.)“可以”运行。 所有代码均已准备好文档。
我在我们的项目中见过类似的代码,但是它真的能保证调用最后一行按钮(“disable”)吗?
$("#saveButton").on("click", function () { $("#myForm").submit(); $("#saveButton").button("disable"); });
替代方案是这样的(如果按钮被禁用,这可能会出现问题,但最后一行也可能会失败,所以它并不真正“感觉良好”。
$("#saveButton").on("click", function () { $("#saveButton").button("disable"); $("#myForm").submit(); });
这可能是最好的方法,对吧?会不会有什么“陷阱”?
$("#myForm").submit(function () { $("#saveButton").button("disable"); // disable also all the elements that can do a submit return true; });
最佳答案
提交几乎是最好的方法,“一次”方法只会给你一次机会,如果你的提交失败或没有验证你被卡住了
$('#myForm').on('submit',function () {
$('#saveButton').button('disable');
// disable also all the elements that can do a submit
//Validate
if (validate)
return true;
else {
//cancels submit
$('#saveButton').button('enable');
return false;
}
});
关于jQuery - 禁用提交按钮的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18488483/