我很想绑定到submit
包含表单的事件,并使用 _.debounce中的概念来限制提交的重复:
jQuery:
// prevent the user from submitting the form twice within a second
var DELAY = 1000;
var lastSubmit = null;
$("#form").submit(function(e) {
if (lastSubmit === null || Date.now() - lastSubmit > DELAY)
return true;
// two ways to prevent the default action
e.preventDefault();
return false;
});
或者直接使用 JavaScript:
// prevent the user from submitting the form twice within a second
var DELAY = 1000;
var lastSubmit = null;
document.getElementById('form').addEventListener('submit', function(e) {
if (lastSubmit === null || Date.now() - lastSubmit > DELAY)
return true;
// two ways to prevent the default action
e.preventDefault();
return false;
});
您可以调整DELAY
以满足您的要求,或处理未能重置禁用状态的表单提交。
如果要求使用钩子的属性,则:
echo "<form onsubmit=\"var stop = lastSubmit && Date.now() - lastSubmit <= 1000;stop && e.preventDefault();return !stop;\">";