dongxun3777 2013-09-19 11:45
浏览 58
已采纳

多个javascript确认

I have a form, on the first 3 input fields (fname, lname, addr) there is an onkeyup function which checks the inputs value against a database. If there is a single match, the function offers a popup (javascript confirm) which asks if the record in the database is correct, if so will prepopulate the fields.

Problem is, most people can type faster than the function can check. If the function finds a match with "smi" but the users types "smith" before the pop up, then the confirm will appear 3 times consecutively.

What can be done about this?

Here's what I've tried, no luck

    function startAjax(){
    if(document.getElementById("flag").value == "yes"){
        if (window.XMLHttpRequest){ xmlhttpp=new XMLHttpRequest(); }else{ xmlhttpp=new ActiveXObject("Microsoft.XMLHTTP"); }
        xmlhttpp.onreadystatechange=function(){
            if(xmlhttpp.readyState==4 && xmlhttpp.status==200){
                    var status = xmlhttpp.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
                    var fname = xmlhttpp.responseXML.getElementsByTagName('fname')[0].firstChild.nodeValue;
                    var addr = xmlhttpp.responseXML.getElementsByTagName('addr')[0].firstChild.nodeValue;
                    var lname = xmlhttpp.responseXML.getElementsByTagName('lname')[0].firstChild.nodeValue;
                    var city = xmlhttpp.responseXML.getElementsByTagName('city')[0].firstChild.nodeValue;
                    var state = xmlhttpp.responseXML.getElementsByTagName('street')[0].firstChild.nodeValue;
                    var zip = xmlhttpp.responseXML.getElementsByTagName('zip')[0].firstChild.nodeValue;
                    var email = xmlhttpp.responseXML.getElementsByTagName('email')[0].firstChild.nodeValue;

                    document.getElementById("ajax_status").innerHTML=status;
                    if(status == "Found user"){
                        document.getElementById('pop_display').style.display = "block";
                        var confirmMsg = "*User Found*

Name: "+fname+" "+lname+"
Address: "+addr+"
City: "+city+"
State: "+state+"
Zip: "+zip+"
Email: "+email+"

Click 'OK' To populate fields or click 'Cancel' if this is not the correct info.";
                        var fillOrNot = confirm(confirmMsg);
                        if(fillOrNot === true){                     
                            document.getElementById('fname').value = fname;
                            document.getElementById('lname').value = lname;
                            document.getElementById('address').value = addr;
                            document.getElementById('city').value = city;
                            document.getElementById('state').value = state;
                            document.getElementById('zip').value = zip;
                            document.getElementById('email').value = email.trim();
                            document.getElementById('flag').value="no";
                        }else{
                            document.getElementById('flag').value="yes2";
                            document.getElementById("ajax_status").innerHTML="Aborted";
                        }
                        document.getElementById('pop_display').style.display = "none";
                    }                   
            }
        }
        var param1 = document.getElementById('fname').value;
        var param2 = document.getElementById('lname').value;
        var param3 = document.getElementById('address').value;
        var url = "https://www.mywebsite.com/ajaxhandler.php?fname="+param1+"&lname="+param2+"&addr="+param3;
        xmlhttpp.open("GET",url,true);
        xmlhttpp.send(null);
    }
}
  • 写回答

2条回答 默认 最新

  • doufuhao0566 2013-09-19 12:12
    关注

    Use 'blur' of javascript or 'onfocusout' of jQuery instead of 'onkeyup'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!