Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript不重定向页面_Javascript - Fatal编程技术网

Javascript不重定向页面

Javascript不重定向页面,javascript,Javascript,我有以下html代码: <html> <head> <script type="text/javascript" src="buttonClicked.js"></script> </head> <body> <form id="confirmChangeId" name="selectionForm" method="POST">

我有以下html代码:

<html>
    <head>
        <script type="text/javascript" src="buttonClicked.js"></script>
    </head>
    <body>
        <form id="confirmChangeId" name="selectionForm" method="POST">
            <input id="btnChange" type="submit" value="Change">
        </form>
    </body>
</html>

使用Firefox,当JS文件中未注释警报时,就会发生重定向。在使用Chrome或Opera时,不会发生任何重定向。很明显,我遗漏了一些小东西,但找不到它是什么。建议?

如果希望重定向可靠地工作,必须添加
返回false在onlick函数的末尾

注意:您应该将事件侦听器附加到对象,而不是使用过时的
onclick
属性

var change = document.getElementById("btnChange");
change.addEventListener('click', function(event) {
    event.preventDefault(); // stops the click from completing
    window.location.href='http://stackoverflow.com';
}, false);

添加
返回false在您的function.otherwaise表单中,在重定向之前提交

 var initFunc = function() {
 var change = document.getElementById("btnChange");
 change.onclick=function() {
    window.location.href='http://stackoverflow.com';
    return false;
    //alert("change button clicked"); // If this line is uncommented, then the page re-         direction does occur
  }
 }

下面的代码应该可以工作。必须将return false添加到onlcik函数中。您需要事件侦听器

见下文:

<script>
window.onload = function() {
    initFunc();
}

var initFunc = function() {
    var change = document.getElementById("btnChange");
change.addEventListener('click', function(event) {
    event.preventDefault(); // stops the click from completing
    window.location.href='http://stackoverflow.com';
}, false);
}
</script>

<html>
    <head>

    </head>
    <body>
        <form id="confirmChangeId" name="selectionForm" method="POST">
            <input id="btnChange" type="submit" value="Change">
        </form>
    </body>
</html>

window.onload=函数(){
initFunc();
}
var initFunc=函数(){
var change=document.getElementById(“btnChange”);
change.addEventListener('click',函数(事件){
event.preventDefault();//停止完成单击
window.location.href=http://stackoverflow.com';
},假);
}

是否尝试将按钮类型从“提交”更改为“按钮”?出现的情况是,没有任何东西阻止您提交表单。或者,阻止表单提交。change.onclick=function(e){e.preventDefault();//其他东西}谢谢大家。加上下面的Michaels答案,这是一个更加优雅的解决方案!发出警报只是为了确认发生了什么事。这是一个完全的侥幸,它实际上允许页面重定向。谢谢迈克尔。JS新手把我的头发拔了太长时间,试图让这个工作!
<script>
window.onload = function() {
    initFunc();
}

var initFunc = function() {
    var change = document.getElementById("btnChange");
change.addEventListener('click', function(event) {
    event.preventDefault(); // stops the click from completing
    window.location.href='http://stackoverflow.com';
}, false);
}
</script>

<html>
    <head>

    </head>
    <body>
        <form id="confirmChangeId" name="selectionForm" method="POST">
            <input id="btnChange" type="submit" value="Change">
        </form>
    </body>
</html>