gpt4 book ai didi

Javascript 转到 URL 如果用户输入特定数字,否则转到另一个 url

转载 作者:行者123 更新时间:2023-12-03 11:02:46 25 4
gpt4 key购买 nike

我希望如果用户在 id input-code 上输入的内容仅为 1234abc,那么他将被重定向到 Google。如果他输入其他内容,那么他​​将被重定向到 Bing。

我的 HTML:

<input type="text" placeholder="Enter Your Code" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Your Code'" maxlength="10" id="input-code">
<p><a href="#" class="btn" onclick="return btntest_onclick()">VERIFY NOW</a></p>

我的 JavaScript:

    if (document.getElementById('input-code').value == '1234'){
function btntest_onclick(){
window.location.href = "http://www.google.com";
}
btntest_onclick()
}else{
function btntest_onclick(){
window.location.href = "http://www.bing.com";
}
btntest_onclick()
}

但是使用此代码,当我转到我的页面时,它会立即将我重定向到 Bing。

最佳答案

您声明了同一个函数两次,因此它正在运行最后定义的版本。而且因为您的代码不在函数中或绑定(bind)到事件,所以它在页面加载时运行。试试这个:

function btntest_onclick(){
if (document.getElementById('input-code').value == '1234') {
window.location.href = "http://www.google.com";
} else {
window.location.href = "http://www.bing.com";
}
};

关于Javascript 转到 URL 如果用户输入特定数字,否则转到另一个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007267/

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