我在同一页面上有两个表单,我已经上载了一个脚本(不记得从哪里来),该脚本可以正常工作,但是在提交两个表单时都会调用它,我希望它确定提交哪个表单并执行其他操作对于第二种形式,它们都需要具有complete:function
表格1
<form action="upload.php" name="upform" method="post" enctype="multipart/form-data" onsubmit="return disuload();">
<label for="ftp_upload" id="uloadlfile">Select a file: </label><input type="file" name="ftp_upload" id="uloadfile"></br>
<label for="ftp_upcomm" id="uloadlcomm" class="percent">Comments: </label><input type="text" name="ftp_upcomm" size="34" id="uloadcomm"></br>
<label id="uloadlimit">Maximum upload size: <?php echo ini_get('post_max_size'); ?></label></br>
<input type="submit" id="uloadsub" value="Upload">
</form>
表格2
<form method="post" name="NewFolder">
<label for="ftp_nfolder">Folder name: </label><input type="text" name="ftp_nfolder" size="30"></br>
<input type="submit" value="Create">
</form>
脚本
(function() {
var bar = $('.bar');
var percent = $('.percent');
$('form').ajaxForm({
beforeSend: function() {
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal + ' uploaded, Please wait...');
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal + ' uploaded, Please wait... ');
},
complete: function(xhr) {
bar.width("0%");
location.reload();
percent.html(xhr.responseText);
}
});
})();
最佳答案
您正在使用
$('form')
适用于文档中的每个表单元素。您可以:
分别为特定表单添加ajaxForm,例如:
$('form [name =“ NewFolder”'])。ajaxForm({// ...
$('form [name =“ upform”]')。ajaxForm({// ...
要么
2.确定回调内的表单名称:
//(...)
complete: function(xhr) {
if ('NewForm' === this.name) {
//... do something
} else {
//Somethingelese
}
//(...)