如果用户输入一个特定的号码,则转到另一个网址 [英] Javascript Go To A URL If user input a specific number else go to another url

查看:121
本文介绍了如果用户输入一个特定的号码,则转到另一个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果用户输入的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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆