如果用户输入一个特定的号码,则转到另一个网址 [英] Javascript Go To A URL If user input a specific number else go to another url
问题描述
我想如果用户输入的id input-code
只有 1234abc
,那么他会被重定向去谷歌。如果他输入了其他内容,那么他将被重定向到Bing。
我的HTML:
< input type =textplaceholder =输入您的代码onfocus =this.placeholder =''onblur =this.placeholder ='输入您的代码'maxlength =10 ID = 输入代码 >
< p>< a href =#class =btnonclick =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。 你声明了两次相同的函数,所以它正在运行最后定义的版本。而且因为你的代码不在函数中或者绑定到它在页面加载时运行的事件。试试这个:
pre code> 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;
}
};
I want if user input on id input-code
is only 1234abc
, then then he'll be redirected to Google. If he enter anything else, then he'll be redirected to Bing.
My 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>
MY 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()
}
But with this code, when I go to my page, it instantly redirect me to Bing.
You're declaring the same function twice, so it is running the last defined version. And because your code is not in a function or bound to an event it runs on page load. Try this:
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";
}
};
这篇关于如果用户输入一个特定的号码,则转到另一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!