在我的网站上有一个未显示的框。当用户输入用户名和密码时,如果正确,则必须显示该框。您可以在此处看到 HTML 代码:
<div id="mainbox" style="display: none;">
<p class="classm">Test.</p>
</div>
<input type="text" id="user"/>
<br />
<input type="text" id="password" value="a"/>
<br />
<input type="button" value="Log in" onclick="login();">
这里是 javascript 函数:
function login() {
var a = document.getElementById('password').value;
var b = document.getElementById('user').value;
if ((a == 'qwe') && (b == 'rty')) { //the problem is not here
document.getElementById('password').style.display="none";
}
else
{
alert('You are not logged in.');
}
}
当我单击按钮登录
时,函数login();
由于发生任何事情而没有被调用。你知道为什么吗?
最佳答案
if (a == 'qwe') && (b == 'rty') //Wrong
将条件括起来,例如 if( condition )
if ((a == 'qwe') && (b == 'rty') ) //Corect
正如 @Frédéric Hamidi 所建议的,不需要内括号。
就这么简单
if (a == 'qwe' && b == 'rty' )
演示 here
关于Javascript 改变输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547313/