验证后链接到不同的页面

时间:2014-05-19 06:36:12

标签: javascript php html

这是我的注册页面代码。我想知道如何在表单验证功能之后链接到另一个页面?

<html>
    <head>
    <head>
    <script type="text/javascript">
    function formValidator()
        {

            var position=document.getElementById("position");

                if(madeSelection(position,"chose a position"))
                {
                        {
                            alert("Correct");
                            return true;
                        }
                    }
                        return false;


                }



    function madeSelection(elem, helperMsg)
    {
        if(elem.value == "-------")
        {
            alert(helperMsg);
            elem.focus();
            return false;
        }
        else if (elem.value == "student")
        {
                 window.location.href="http://www.newpage.com";
            return true;
        }
        else
        {
                 window.location.href="http://www.newpage.com"; 
            return true;
        }
    }

    </script>
    </head>
    <body>
    <form onsubmit="return formValidator()" method="get">
    <p> I'm a : 
    <select id="position">
        <option>-------</option>
      <option>Student  </option> 
  • //如果选择学生,它将进入学生注册页面

      <option>Lecturer</option>  
    
  • //如果选择此项将进入验证然后链接到 讲师注册页面..但如何?

    </select></p>
    <input type="submit" value="Next" />    
    </form>
    </body>
    </html>
    

3 个答案:

答案 0 :(得分:0)

//try this:
window.location.assign('somepage.php');

答案 1 :(得分:0)

您需要将代码更改为:

<html>
    <head>
    <head>
    <script type="text/javascript">
    function formValidator()
        {

            var position=document.getElementById("position");

                if(madeSelection(position,"chose a position"))
                {
                        {
                            alert("Correct");
                            return true;
                        }
                    }
                        return false;


                }



    function madeSelection(elem, helperMsg)
    {
        if(elem.value == "-------")
        {
            alert(helperMsg);
            elem.focus();
            return false;
        }
        else if (elem.value == "student")
        {
                 window.location.href="http://www.google.com";
            return true;
        }
        else if (elem.value == "lecturer")
        {
                 window.location.href="http://www.bing.com";
            return true;
        }
        else
        {
                 window.location.href="http://www.newpage.com"; 
            return true;
        }
    }

    </script>
    </head>
    <body>
    <form onsubmit="return formValidator()" method="get">
    <p> I'm a : 
    <select id="position">
        <option>-------</option>
      <option value="student">Student</option>
       <option value="lecturer">Lecturer</option>  
       </select></p>
<input type="submit" value="Next" />    
</form>
</body>
</html>

答案 2 :(得分:0)

<input type="radio" name="choose" id="student">student</br>
<input type="radio" name="choose" id="lecturer">lecturer</br>

<button onclick="doit()">click here</button>


<script>
function doit(){
var rad1 = document.getElementById("student");
var rad2 = document.getElementById("lecturer");

if(rad1.checked){
    window.location.assign('test1.php');
}
else if(rad2.checked){
    window.location.assign('test2.php');
}
else{
    window.load();
}


}
</script>