JS 將form表單與上傳檔案 一起提交
阿新 • • 發佈:2019-02-10
前提 需要引入jquery(不然無法使用$.ajax)
//js
function add(){
var formData = new FormData(document.querySelector("form"));//獲取form值
$.ajax({
url: "http://chetest.svell.cn/jfinal_demo/student/test",
type: "POST",
data: formData,
processData: false, // 不處理資料
contentType: false , // 不設定內容型別
success:function(data){
alert(data);
}
});
}
//html
<form>
<input type="text" name="name">
<input type="text" name="age">
<input type="text" name="phone">
<input type="file" name="file">
<input type ="button" onclick="add()" value="新增">
</form>