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

如果用户输入一个特定的数字,Javascript将转到一个URL,否则将转到另一个URL,javascript,html,input,Javascript,Html,Input,我想如果用户在idinput code上输入的内容只有1234abc,那么他就会被重定向到谷歌。如果他输入其他内容,他将被重定向到Bing 我的HTML: <input type="text" placeholder="Enter Your Code" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Your Code'" maxlength="10" id="input-code"> <p&g

我想如果用户在id
input code
上输入的内容只有
1234abc
,那么他就会被重定向到谷歌。如果他输入其他内容,他将被重定向到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>

但是有了这段代码,当我转到我的页面时,它会立即将我重定向到Bing。

您两次声明相同的函数,因此它运行的是最后定义的版本。由于代码不在函数中或未绑定到事件,因此它在页面加载时运行。试试这个:

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";
    }
};

if和else块中的函数声明很奇怪。这是因为函数在页面加载时立即执行。永远不要在JavaScript中有条件地声明函数。
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";
    }
};