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

时间:2016-10-17 15:03:42

标签: javascript

您好我试图验证用户输入一些数据,然后将用户重定向到其他网页,警报效果不错,但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';
        }
    }
}    

感谢。

解决方案:

我需要的只是在位置之后添加return false;以停止脚本并继续重定向指令,感谢所有回复。

2 个答案:

答案 0 :(得分:1)

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';
    }
}

}

更改了一些变量并添加了" /"到window.location.href

答案 1 :(得分:0)

我将您的代码更改为此并使其工作正常:

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';
        }
    }
}

我将valor变量更改为data,将envia更改为send,将window.location.href更改为window.location