簡體   English   中英

Javascript .onclick 函數重定向到其他頁面

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

嗨,我正在嘗試驗證用戶輸入的一些數據,然后將用戶重定向到其他網頁,警報效果很好,但 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; 在停止腳本並繼續重定向指令的位置之后,感謝大家的回復。

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 中添加了一個“/”

我把你的代碼改成了這個,讓它工作得很好:

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變量dataenviasend ,並window.location.hrefwindow.location

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM