gpt4 book ai didi

javascript - ajax不同形式同一页面

转载 作者:行者123 更新时间:2023-12-03 12:30:28 25 4
gpt4 key购买 nike

我在同一页面上有两个表单
我已经采取了一个上传脚本(不记得从哪里来),它工作正常,但在提交两个表单时都会调用它,我希望它确定哪个表单已提交,并对第二个表单执行不同的操作。
它们都需要具有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')

这适用于文档中的每个表单元素。您可以:

  1. 单独为特定表单添加ajaxForm,例如:

    $('form[name="NewFolder"']).ajaxForm({//...

    $('form[name="upform"]').ajaxForm({//...

或者2. 确定回调中表单的名称:

//(...)
complete: function(xhr) {
if ('NewForm' === this.name) {
//... do something
} else {
//Somethingelese
}
//(...)

关于javascript - ajax不同形式同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com