项目的需求就是不能重复提交
是通过click点击事件提交数据,到这里的时候想了很多办法,比如说销毁这个点击事件,
但是这个实现起来是相对比较麻烦的,后面想到了一个相对比较简单的办法
通过推荐判断
JS
var isFirst=true;//先设定条件为true
if(isFirst){//当条件为真的时候执行 tijiao.onclick=function(){ var comboId=sessionStorage.getItem("comboId"); console.log(comboId); var shuliang=document.getElementById("endNum").innerHTML; console.log(shuliang); var zonjia=document.getElementById("allMoney").innerHTML*100; console.log(zonjia); var shouhuoren=document.getElementById("name").value; console.log(shouhuoren); var dianhua=document.getElementById("phone").value; console.log(dianhua); var dizhi=document.getElementById("address").value; console.log(dizhi); if(comboId!==null&shuliang!==null&zonjia!==null&shouhuoren!==null&dianhua!==null&dizhi!==null){ var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.open("post", "http://www.weifen168.com//api/domainFormProductComboOrder/createOrder"); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("comboId="+comboId+"&orderPrice="+zonjia+"&orderNum="+shuliang+"&name="+shouhuoren+"&mobile="+dianhua+"&address="+dizhi+"&payType=1"); xhr.onreadystatechange = function () { if (xhr.readyState==4&&xhr.status==200) { console.log(xhr.responseText); isFirst=falze;//提交成功之后再把isFirst条件改变为false alert('提交成功') } } }else{alert("请输入完整信息")} return isFirst //返回 } }else{//当点击事件再次执行的时候,条件为false alert('请勿重复提交') }