Javascript 改变输入值

标签 javascript html

在我的网站上有一个未显示的框。当用户输入用户名和密码时,如果正确,则必须显示该框。您可以在此处看到 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/

相关文章:

javascript - 将 OO 语言编译为 Javascript

javascript - 无法从 angular4 中的嵌套函数访问它

javascript - 无法将我的 JS 文件链接到我的 HTML 页面

asp.net - 将 div 中的 HTML 保存为图像

javascript - 正则表达式在可选字符之前获取 URL 中的字符串

JavaScript 函数只有 setTimeout 才能工作

javascript - Javascript "Math.sin"和 WebGL“sin”之间的区别

javascript - 如何将首次访问者重定向到另一个页面?

html - 将鼠标悬停在图片上时如何在新的 div 中打开图片?

javascript - 重新组织 Bootstrap 布局 - 通过单击扩展 div 时的列排序