gpt4 book ai didi

javascript - 如何使 JQuery 文件上传插件在未选择文件的情况下工作?

转载 作者:行者123 更新时间:2023-11-27 23:59:11 33 4
gpt4 key购买 nike

我知道有人问过这个问题 before并且没有给出满意的答复。我正在使用 sails.js 开发网站。

我有一个带有一些文本字段和文件上传选项的表单(我使用了 JQuery 文件上传插件)。但是,如果未选择任何文件并且仅输入文本字段,则提交不会进一步进行。

这是我的 main.js 提交函数:

$(function () {
'use strict';
var propertyId = document.getElementById("helper").getAttribute("data-name");
var fileCount = 0, fails = 0, successes = 0;
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
//url: 'server/php/'
singleFileUploads: false,
url: '/admin/property/'+propertyId+'/pictures/upload'
})

这是我的表格:

<form id="fileupload" action="/admin/property/<%=propertyId%>/pictures/upload" method="POST" enctype="multipart/form-data">
<noscript><input type="hidden" name="redirect" value="https://blueimp.github.io/jQuery-File-Upload/"></noscript>
<div class="row fileupload-buttonbar">
<div class="col-lg-7">
<label>Stage Title:</label>
<input class="form-control" type="text" name="stageTitle"/>
<br>
<label>Start Date:</label>
<input class="form-control" type="text" name="startDate"/>
<br>
<label>End Date:</label>
<input class="form-control" type="text" name="endDate"/>
<br>
<label>Description:</label>
<textarea class="form-control" type="text" name="description"> </textarea>
<br>
<label>Pictures:</label><br>
<span class="btn btn-success fileinput-button">
<span>Add Images...</span>
<input type="file" name="files" id="files" multiple>
</span>

<button type="reset" class="btn btn-warning cancel">
<span>Cancel upload</span>
</button>

<span class="fileupload-process"></span>
</div>
<div class="col-lg-5 fileupload-progress fade">
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar progress-bar-success" style="width:0%;"></div>
</div>
<div class="progress-extended">&nbsp;</div>
</div>
<button type="submit" class="btn btn-primary start">
<span>Save</span>
</div>

<table id="pictureTable" role="presentation" class="table table-striped"><tbody class="files"></tbody></table>

</button>
</form>

如何使库在没有选择文件的情况下工作?另一种选择是进行两次单独的提交,一次针对文本字段,一次针对文件。但这会很困惑,我不喜欢那样。任何指导将不胜感激。

最佳答案

只需检查文件是否已设置:

if($('#fileupload').val() != ""){
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
//url: 'server/php/'
singleFileUploads: false,
url: '/admin/property/'+propertyId+'/pictures/upload'
});
}

关于javascript - 如何使 JQuery 文件上传插件在未选择文件的情况下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31976755/

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