gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 16:27:41 25 4
gpt4 key购买 nike

您好,我正在尝试验证用户输入一些数据,然后将用户重定向到其他网页,警报工作正常,但 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 添加了“/”

关于Javascript .onclick 函数重定向到其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40089800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com