链接:https://www.cnblogs.com/chwlhmt/p/8439135.html
(侵删)
FormData
这是HTML5 中新增的API
优点:FormData不仅能读取表单数据,也能自行追加数据
html:
<form name="form1" id="form1">
<p>name:<input type="text" name="name" /></p>
<p>gender:<input type="radio" name="gender" value="1" />male <input type="radio" name="gender" value="2" />female</p>
<p>stu-number:<input type="text" name="number" /></p>
<p>photo:<input type="file" name="photo" id="photo"></p>
<p><input type="button" name="b1" value="submit" onclick="fsubmit()" /></p>
</form>
js:
function fsubmit() {
var form=document.getElementById("form1");
var fd =new FormData(form);
$.ajax({
url: "server.php",
type: "POST",
data: fd,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
success: function(response,status,xhr){
console.log(xhr);
var json=$.parseJSON(response);
}
});
return false;
}
判断值
html:
<form id="formid" name="myform" method='post' onsubmit="return checkUser();" >
<input type="text" value="" class="text2" name="username" id="userid"/></td> <input type="password" value="" class="text2" name="userpass" id="userpassid"/></td>
<input type="submit" value="" class="btn2" />
</form>
js:
function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("passid").value;
if(!result){
alert("用户名不能为空");
return false;
}
if(!password){
alert("密码不能为空");
return false;
}
document.getElementById("formid").submit();
}