gpt4 book ai didi

javascript - 仅发送非空的帖子文件

转载 作者:行者123 更新时间:2023-11-28 15:45:56 25 4
gpt4 key购买 nike

我的表单上有三个输入文件:

<td><input type="file" name="image_url1"/></td>
<td><input type="file" name="image_url2"/></td>
<td><input type="file" name="image_url3"/></td>

我只想发送(在邮寄时)我们选择的文件(保持为空的输入文件 => 不发送它们)。

我怎样才能做到这一点?

最佳答案

您可以使用.length检查文件长度,并禁用空控件。这样它们就不会被发布。

 $('input[type=file]').each(function () {
if (this.value == "" || this.files.length == 0) {
this.prop('disabled', true)
}

});

更新:如果您不使用 jQuery,您可以通过简单的 javascript 来完成此操作,如下所示:

var myFiles = document.querySelectorAll("input[type=file]");
for(var i = 0; i<myFiles.length; i++)
{
if(myFiles[i].value == "" || myFiles[i].files.length == 0)
{
myFiles[i].disabled = true;
}
}

关于javascript - 仅发送非空的帖子文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22303543/

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