我正在构建一个相当大的表单。用户可以一次性上传图像。仅提交输入文件jQuery表单插件
该图像应立即上传。然后用户应该继续填写表格。
HTML
<form name="registration" id="registration" action="confirm.php" method="post">
<input name="firstname" id="firstname" type="text" />
<input name="lastname" id="lastname" type="text" />
<!-- file upload -->
<input name="file" id="file" type="file" />
<input name="newsletter" type="checkbox" id="newsletter" />
<input name="captcha" id="captcha" type="tel" />
</form>
的Javascript(jQuery的)
//upload file on change event
$('#file').change(function() {
// put the image object into a variable
// formData is incomaptible with IE9/8/7
fileData = new FormData();
fileData.append('file', this.files[0]);
var options = {
// XMLHttpRequest Level 2 - no cross-browser support
data: fileData,
url: 'upload.php', // override standard url for this call only
type: 'post'
};
// make an ajax call to post the image
$.ajax(options);
// Alternatively use the jQuery Form plugin
// $('#registration').ajaxSubmit(options);
});
不幸的是,jQuery的表单插件http://malsup.com/jquery/form/#file-upload提交整个表单,当我想只提交的输入文件中的字段。
此外,我宁愿避免在我的HTML标记中创建多个单独的表单,因为我还需要处理并提交多个表单。
我在这里错过了什么?
<! - 文件上传'-'>应该是<! - 文件上传'--'> – Salim
评论标记已被修复。 –
您在窗体标记中缺少enctype ='multipart/form-data'属性 –