zoukankan      html  css  js  c++  java
  • JS 一次性事件问题

    项目的需求就是不能重复提交

    是通过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('请勿重复提交')
                            }
  • 相关阅读:
    技巧:在Silverlight中如何访问外部xap文件中UserControl
    Silverlight:代码隐藏和分部类
    silverlight 反射调用WebService
    [推荐]Silverlight 2 开发者海报
    轻松建立Silverlight开发环境
    windows 卸载IE8还原IE7的方法
    如何:访问双工服务
    技巧:在Silverlight 2应用程序中切换用户控件
    如何减少silverlight XAP包的尺寸
    Silverlight:应用程序和编程模型
  • 原文地址:https://www.cnblogs.com/xxflz/p/10461267.html
Copyright © 2011-2022 走看看