form表单通过ajax传
//添加到购物车中
function toCar(){
var id = getCaption(window.location.search,1);
var form = new FormData(document.getElementById("upfile"));
var t1 = false;
var t2 = false;
for (var key of form.keys()) {
//console.log("key:"+typeof(key));
if(key.length != 0){
t1 = true;
}
}
for (var value of form.values()) {
//console.log("value:"+typeof(value));
if(value.length != 0){
t2 = true;
}
}
if(t1&&t2){
form.append("fun",'toCar');
form.append("iwindowsId",id);
$.ajax({
url:"controller/shopcar.php",
type:"post",
data:form,
cache : false,
processData:false,
contentType:false,
dataType : "json",
success:function(result){
if(result.state == "ok"){
alert("提交成功!");
window.location.href = 'seeFood.html?id='+id;
}else{
alert("提交失败!");
}
}
});
}else{
alert("请先选择");
}
}
<form id="upfile">
<div class="row" id="selectShow">
<!--内部的东西由其他的ajax传进来-->
</div>
</form>
为了让js有序的输出,我们可以使用回调的方式。