繁体   English   中英

我如何更改JavaScript的输出以直接转到另一个页面而不是警报窗口?

[英]how can i change the output of JavaScript to go to directly to another page instead of an alert window?

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Validate Zip Code</title>
            <script type="text/javascript">
                function IsValidZipCode(zip) {
                    var isValid = /20132/.test(zip);
                    if (isValid)
                        alert('Valid ZipCode');
                    else {
                        alert('yep')
                         }
                }
            </script>
        </head>


   <body>
    <form>
    <input id="txtZip" name="zip" type="text" /><br />
    <input id="Button1" type="submit" value="Check My Zipcode"
    onclick="IsValidZipCode(this.form.zip.value)" />
    </form>
    </body>
    </html>

我需要使用它来允许用户进入对不起我们无法为您服务的区域的页面,或进入对我们可以根据其邮政编码列出您可以为您的区域服务的页面。

还如何在isValid =行中添加多个邮政编码?

<script type="text/javascript">
            function IsValidZipCode(zip) {
                var isValid = /20132/.test(zip);
                if (isValid)
                  window.location = baseurl."/valid.php"
                else {
                    window.location = baseurl."/invalid.php"
                     }
            }
</script>

设置window.location.href = "your url here"; 回答问题的第一部分。

“还如何在isValid =行中添加多个邮政编码?”

如果要坚持使用正则表达式测试,可以使用正则表达式或|

var isValid = /^(20132|20133|20200|90210|etc)$/.test(zip);

请注意,我还添加了^$以匹配输入字符串的开头和结尾-如果用户输入了包含该代码的较长字符串,例如, "abc20132xyz"将匹配,则您也将获得匹配。

不过,我更倾向于在服务器端进行此验证。

if (isValid)
    document.location.href="validzipcode.html";
else {
    document.location.href="yep.html";
}
function IsValidZipCode(zip) {
    var isValid = /20132/.test(zip);
    if (isValid)
        document.location.href="valid_zip.html";
    else {
        document.location.href="not_valid_zip.html";
    }
}

您可以使用以下代码在javascript中重定向它,该代码可以放在示例中,而不是放在alert()中

window.location.href = "http://stackoverflow.com";

您可以为此尝试使用ajax:

<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Validate Zip Code</title>



<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function IsValidZipCode(zip){

    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

        //alert(ajaxRequest);
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){


        if(ajaxRequest.readyState == 4){
            //var ajaxDisplay = document.getElementById('ajaxDiv');
            //ajaxDisplay.innerHTML = ajaxRequest.responseText;
                        alert(ajaxRequest.responseText);
                        if(ajaxRequest.responseText=="")
                        {
                                alert("sorry we cannot service your area");
                        }
                        else{
                                window.location = "Page.php?zip="+zip+"";
                        }

        }
    }
    //var age = document.getElementById('age').value;
    //var wpm = document.getElementById('wpm').value;
    //var sex = document.getElementById('sex').value;

    var queryString = "?zip=" + zip;

    ajaxRequest.open("GET", "zip_check.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



        </head>


   <body>
    <form name="form1" method="post">
    <input id="txtZip" name="zip" type="text" /><br />
    <input type="button" id="Button1"  value="Check My Zipcode"
    onclick="IsValidZipCode(document.form1.zip.value)" />
    </form>
    </body>
    </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM