[英]How to update a form and submit using jquery and ajax
I am trying to submit a form using Ajax, but my form contains a file field and a text field. 我正在尝试使用Ajax提交表单,但是我的表单包含一个文件字段和一个文本字段。 The file field fails but the text field work 文件字段失败,但文本字段有效
THE FORM 表格
<form id="page" enctype="multipart/form-data" >
<input type="text" name="text" id="text">
<input type="file" name="image" id ="image">
<button>Submit</button>
</form>
THE JS SCRIPT FOR SUBMISSION 提交的JS脚本
$('#page').validate({
rules: {
image:{
required: true
},
text:{
required: true
}
},
messages: {
image: {
required: "required"
},
text: {
required: "required"
}
},
submitHandler : function(){
$.ajax({
type: "POST",
cache:false,
url: "finish.html",
data: $('#page').serialize(),
success: function(data) {
alert(data);
}
});
});
Im really new to javascript and jquery, please I want to know where I am going wrong. 我对javascript和jquery真的很陌生,请我想知道我要去哪里了。 Thanks for helping. 感谢您的帮助。
Try this, It may help 试试这个,可能会有所帮助
<input type="file" name="fileToUpload" id="fileToUpload" />
<input type="button" value="Save" onclick="savepic()" />
//you can also use event listeners instead of onclick
jQuery Code jQuery代码
<script type = "text/javascript">
function savepic() {
//can perform client side field required checking for "fileToUpload" field
$.ajaxFileUpload({
url: 'doajaxfileupload.php',
secureuri: false,
fileElementId: 'fileToUpload',
dataType: 'json',
success: function(data, status) {
if (typeof(data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(msg); // returns location of uploaded file
}
}
},
error: function(data, status, e) {
alert(e);
}
})
return false;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.