简体   繁体   English

Javascript .onclick 函数重定向到其他页面

[英]Javascript .onclick function to redirect to other page

Hi i'm trying to validate user typing some data and then redirect the user to other web page, the alerts works nice but the location.href is doing nothing, please help.嗨,我正在尝试验证用户输入的一些数据,然后将用户重定向到其他网页,警报效果很好,但 location.href 什么也没做,请帮忙。

window.onload = function(){
    var send = document.getElementById('send');
    var email = document.getElementById('email');       
    var pass = document.getElementById('pass');

    send.onclick = function(){     
        var data1 = email.value;
        var data2 = pass.value;

        if(data1==''){
            alert('Please enter an email address');
        }
        else if(data2==''){
            alert('Please enter your password');
        }
        else{
            window.location.href = 'myotherpage.html';
        }
    }
}    

Thanks.谢谢。

Solution:解决方案:

All I needed was to add a return false;我所需要的只是添加一个return false; after the location to stop the script and continue to the redirection instruction, thanks all for the replies.在停止脚本并继续重定向指令的位置之后,感谢大家的回复。

window.onload = function(){
var send = document.getElementById('send');
var email = document.getElementById('email');       
var pass = document.getElementById('pass');

send.onclick = function(){     
    var data1 = email.value;
    var data2 = pass.value;

    if(data1==''){
        alert('Please enter an email address');
    }
    else if(data2==''){
        alert('Please enter your password');
    }
    else{
        window.location.href = '/myotherpage.html';
    }
}

} }

changed some variables and added a "/" to window.location.href更改了一些变量并在 window.location.href 中添加了一个“/”

I changed your code to this and got it working just fine:我把你的代码改成了这个,让它工作得很好:

window.onload = function(){
    var send = document.getElementById('send');
    var email = document.getElementById('email');       
    var pass = document.getElementById('pass');

    send.onclick = function(){     
        var data1 = email.value;
        var data2 = pass.value;

        if(data1==''){
            alert('Please enter an email address');
        }
        else if(data2==''){
            alert('Please enter your password');
        }
        else{
            window.location = 'myotherpage.html';
        }
    }
}

I changed your valor variables to data , envia to send , and window.location.href to window.location .我改变了你的valor变量dataenviasend ,并window.location.hrefwindow.location

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

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