2013-08-31 103 views
0

在我的网站中,我有一个未显示的框。当用户输入他的用户名和密码时,如果他们是正确的,则必须显示该框。在这里,你看到的HTML代码:Javascript更改输入值

<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.'); 
    } 
} 

当我点击按钮Log in貌似功能login();,因为事情发生不叫。你知道为什么吗?

+1

你确定你是在右边的框中键入正确的信吗?第一个箱子需要'rty'和第二个'qwe'。 – putvande

+0

工作正常:http://jsfiddle.net/5dgtz/ – Cherniv

回答

4
if (a == 'qwe') && (b == 'rty') //Wrong 

括像if(condition)

if ((a == 'qwe') && (b == 'rty')) //Corect 

条件如@弗雷德里克哈米迪建议的,有不需要内括号的。

简单这样

if (a == 'qwe' && b == 'rty') 

演示here

+0

我做到了,但它仍然无法正常工作。 +1顺便说一句 –

+4

Nitpicking:'=='比'&&'更优先,所以不需要内部圆括号。 –

+0

@AlbertoRossi,我认为你有一个&b混合,'a'应该是用户,'b'通过,或者?我为这个答案添加了一个演示。一探究竟。 – Sergio

2

使用正确的,如果

if (a == 'qwe' && b == 'rty') 

fiddle