Javascript 验证后链接到其他页面

Javascript 验证后链接到其他页面,javascript,php,html,Javascript,Php,Html,这是我注册页面的代码。我想知道在表单验证功能之后如何链接到另一个页面 <html> <head> <head> <script type="text/javascript"> function formValidator() { var position=document.getElementById("position"); if(made

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

<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> 

函数formValidator()
{
var position=document.getElementById(“position”);
if(进行选择(位置,“选择位置”))
{
{
警惕(“正确”);
返回true;
}
}
返回false;
}
函数madeSelection(elem、helperMsg)
{
如果(元素值==“----”)
{
警报(helperMsg);
元素焦点();
返回false;
}
else if(元素值==“学生”)
{
window.location.href=”http://www.newpage.com";
返回true;
}
其他的
{
window.location.href=”http://www.newpage.com"; 
返回true;
}
}
我是一个:
-------
学生
  • //如果选择学生,将进入学生注册页面

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

    </select></p>
    <input type="submit" value="Next" />    
    </form>
    </body>
    </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.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>

函数formValidator()
{
var position=document.getElementById(“position”);
if(进行选择(位置,“选择位置”))
{
{
警惕(“正确”);
返回true;
}
}
返回false;
}
函数madeSelection(elem、helperMsg)
{
如果(元素值==“----”)
{
警报(helperMsg);
元素焦点();
返回false;
}
else if(元素值==“学生”)
{
window.location.href=”http://www.google.com";
返回true;
}
else if(元素值==“讲师”)
{
window.location.href=”http://www.bing.com";
返回true;
}
其他的
{
window.location.href=”http://www.newpage.com"; 
返回true;
}
}
我是一个:
-------
学生
讲师

学生
讲师
点击这里 函数doit(){ var rad1=document.getElementById(“学生”); var rad2=document.getElementById(“讲师”); 如果(rad1.选中){ window.location.assign('test1.php'); } 否则如果(rad2.已选中){ window.location.assign('test2.php'); } 否则{ window.load(); } }
无法工作。。。单击“学生”或“讲师”后,它只会显示“正确”。它仍然相同,在选定的学生或讲师之后,它无法链接到其他页面。我只会在我的电脑上显示它工作正常。您可以尝试将madeSelection()函数中的“return true;”更改为“return false;”。您的意思是当我将文件名更改为html而不是html时,else if thereworks。。但是为什么呢?你可以通过链接轻松做到:
<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>
<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>