Javascript 根据复选框使用java脚本重定向到页面

Javascript 根据复选框使用java脚本重定向到页面,javascript,html,Javascript,Html,我不知道为什么它不工作,想根据复选框值重定向到页面或什么也不做。这就是代码 <html> <body> <form onsubmit= "lol()" > Checkbox: <input type="checkbox" id="myCheck"> <input type="submit" value="Submit"> </form> <script> function lol() { if(documen

我不知道为什么它不工作,想根据复选框值重定向到页面或什么也不做。这就是代码

<html>
<body>

<form onsubmit= "lol()" >
Checkbox: <input type="checkbox" id="myCheck">
<input type="submit" value="Submit">
</form>

<script>
function lol()
{
if(document.getElementById("myCheck").checked == true)
{
window.location="http://www.google.com";
}
else
{
// want do nothing and stay at same page .
}
}
</script>

</body>
</html>

复选框:
函数lol()
{
if(document.getElementById(“myCheck”).checked==true)
{
window.location=”http://www.google.com";
}
其他的
{
//什么都不想做,只想呆在同一页上。
}
}
如何执行此操作

修改您的功能:

function lol()
{
if(document.getElementById("myCheck").checked == true)
{
window.location.href="http://www.google.com";
}
else
{
// want do nothing and stay at same page .
}
}

要从一个页面重定向到另一个页面,您可以使用
window.location.href
,而不是
window.location

您可以在jquery中执行此操作

$('#myCheck').click(function() {
    if($('#myCheck').is(':checked')){
        window.location = 'http://www.naveedramzan.com';
    }
});

您还可以使用
location.assign()
函数

function lol()
{
if(document.getElementById("myCheck").checked == true)
{
window.location.assign("http://www.google.com");
}
else
{
// want do nothing and stay at same page .
}
}

如果你想保持表单不做任何错误的事情,你需要在这里做两件事

  • 调用函数时,需要使用return。这样表单在得到返回值之前不会提交

  • 在函数的其他部分中,需要提到return=false。它将停止表单提交

  • Javascript:

        function lol()
        {    
         if(document.getElementById("myCheck").checked == true)
         {    
            window.location.href="http://www.google.com";
         }
         else
          {
            return false;
          }
        }
    
    HTML

      <form onsubmit="return lol()">
    Checkbox: <input type="checkbox" id="myCheck"/>
    <input type="submit" value="Submit" />
    </form>
    
    
    复选框:
    

    您不想为此使用帖子,一切都可以在客户端处理:

    <body>
        Checkbox: <input type="checkbox" id="myCheck">
        <input type="button" value="Submit" onclick="lol()">
    
        <script>
            function lol() {
                if (document.getElementById("myCheck").checked === true) {
                    window.location = "http://www.google.com";
                }
                else {
                    // want do nothing and stay at same page .
                    alert("staying on page");
                }
            }
        </script>
    </body>
    
    
    复选框:
    函数lol(){
    if(document.getElementById(“myCheck”).checked==true){
    window.location=”http://www.google.com";
    }
    否则{
    //什么都不想做,只想呆在同一页上。
    警惕(“保持在页面上”);
    }
    }
    
    jQuery不是javascript的扩展吗?啊,好的。要使用jquery,需要包括jquery文件。