Forms 使用jquery提交表单
在提交表单时,我尝试使用id提交表单,并通过JQuery调用submit,但第一个脚本会让我陷入无限循环 在使用plupload之后,我注意到他们使用另一种方式提交表单,这不会导致无限循环 我试图找到有关这方面的信息,但我似乎无法获得有关这方面的具体信息Forms 使用jquery提交表单,forms,submit,Forms,Submit,在提交表单时,我尝试使用id提交表单,并通过JQuery调用submit,但第一个脚本会让我陷入无限循环 在使用plupload之后,我注意到他们使用另一种方式提交表单,这不会导致无限循环 我试图找到有关这方面的信息,但我似乎无法获得有关这方面的具体信息 $('#test').submit(function(e) { alert("here"); $('#test').submit(); return false; }); $('form').submit(function(
$('#test').submit(function(e) {
alert("here");
$('#test').submit();
return false;
});
$('form').submit(function(e) {
alert("here");
$('form')[0].submit();
return false;
});
开始编辑
在第一种情况下,您在jQuery对象上触发jQuery提交事件(请参阅)
在第二种情况下,您正在对javascript对象调用javascript提交方法。它不会触发提交事件
这两种情况下的“提交”并不相同
在第二种情况下,如果更换:
$('form')[0].submit();
与
您还将得到一个无限循环
结束编辑
事实上,使用plupload,关键是等待您能够成功提交表单的时间。主要功能是订阅StateChanged事件
它是这样工作的:
1-首先检查队列是否为空。如果是,请验证表单提交(返回true)
2-如果队列不是空的
a-正在加载剩余文件
b-订阅将来的“StateChanged”事件,它将在其中检查队列。如果此时队列为空,则转到将成功通过的步骤1(因为此时队列将为空)
c-取消表单提交(队列当前不是空的)
一些带有注释的代码:
// Client side form validation
$('form').submit(function(e) {
var myForm = e.currentTarget;
var uploader = $(item).pluploadQueue();
// if files in queue upload them first. Cancel current form submission.
// Subscribe to 'StateChanged' to check if the queue is empty and try
// a new submission, each time a file is finished uploading
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('StateChanged', function() {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
myForm.submit(); //
}
});
// start uploading remaining files.
// End of each upload will trigger the 'StateChanged'
uploader.start();
// there are files in queue. (the reason we are running current block)
// Cancel current form submission
return false;
}
return true; // the queue is empty, validate current form submission
});
希望这会有所帮助谢谢你的示例,我已经创建了一个工作示例,但我注意到表单调用的差异,我想知道为什么我的第二个示例不会像第一个示例那样导致循环,因为页面上还有一个表单?@BartLeemans:谢谢你的解释。我编辑了我的答案。希望这会有所帮助。
// Client side form validation
$('form').submit(function(e) {
var myForm = e.currentTarget;
var uploader = $(item).pluploadQueue();
// if files in queue upload them first. Cancel current form submission.
// Subscribe to 'StateChanged' to check if the queue is empty and try
// a new submission, each time a file is finished uploading
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('StateChanged', function() {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
myForm.submit(); //
}
});
// start uploading remaining files.
// End of each upload will trigger the 'StateChanged'
uploader.start();
// there are files in queue. (the reason we are running current block)
// Cancel current form submission
return false;
}
return true; // the queue is empty, validate current form submission
});