我有一个表单,服务器需要一点时间来处理。我需要确保用户等待,并且不试图通过再次单击按钮重新提交表单。我尝试使用以下jQuery代码:<script type="text/javascript">$(document).ready(function() {
$("form#my_form").submit(function() {
$('input').attr('disabled', 'disabled');
$('a').attr('disabled', 'disabled');
return true;
});});</script>当我在Firefox中尝试这一点时,所有的东西都会被禁用,但是表单并没有与它应该包含的任何POST数据一起提交。我不能使用jQuery提交表单,因为我需要将按钮与表单一起提交,因为有多个Submit按钮,并且我确定在POST中包含了哪个值。我需要提交的表单,因为它通常是,我需要禁用所有的权利后,立即发生。谢谢!防止jQuery中表单的双重提交
3 回答
HUWWW
TA贡献1874条经验 获得超12个赞
怎么做
$('form').submit(function(){
$(this).find(':submit').attr('disabled','disabled');});
幕布斯6054654
TA贡献1876条经验 获得超7个赞
编辑
// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmission = function () {
$(this).on('submit', function (e) {
var $form = $(this);
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
alert('Form already submitted. Please wait.');
e.preventDefault();
} else {
// Mark it so that the next submit can be ignored
// ADDED requirement that form be valid
if($form.valid()) {
$form.data('submitted', true);
}
}
});
// Keep chainability
return this;
};添加回答
举报
0/150
提交
取消
