为什么这不起作用? (HTML / JavaScript的)

时间:2017-03-23 09:12:16

标签: javascript html

我希望这只是检查它是否正确,我不是很聪明的HTML所以我真的希望如果有人可以解释像我是一个小孩,所以我可以理解。 这是代码:



<input type="text" id="user" value="">
<input type="password" id="pass" value="">
<button type="button" id="btn" onclick="func()">Login</button>
<h1 id="txt"></h1>
<script>
  var userstate = "false";
  var passstate = "false";

  function func() {
    var utxt = document.getElementById("user").value;
    var ptxt = document.getElementById("pass").value;
    if (utxt == "david")
      userstate = "true";
    if (ptxt == "lol123")
      passstate = "true";
    if (userstate == "true" && passstate == "true")
      var txt = "Login succesfully";
    else
      var txt = "Login not succesfull";
  }
</script>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您应该使用以下内容将您的登录消息放入id为txt的div中:

document.getElementById('txt').innerHtml = "the text";

<!DOCTYPE html>
<html>
<body>
<input type="text" id="user" value="">
<input type="password" id="pass" value="">
<button type="button" id="btn" onclick="func()">Login</button>
<h1 id="txt"></h1>
<script>
var userstate="false";
var passstate="false";
function func()
{
  var utxt = document.getElementById("user").value;
  var ptxt = document.getElementById("pass").value;
  if (utxt == "david")
    userstate = "true";
  if (ptxt == "lol123")
    passstate = "true";
  if (userstate == "true" && passstate == "true")
    document.getElementById('txt').innerHTML = "Login succesfully";
  else
    document.getElementById('txt').innerHTML =  "Login not succesfull";
}
</script>
</body>
</html>

同时

布尔值(true | false)不需要引号:

您可以简单地使用:

userstate = true;

或者

userstate = fale;

并将其检查为:

if(userstate == false){}

答案 1 :(得分:1)

几个问题

  1. 从false和true中删除引号或使用直接赋值和三元组,如下所示
  2. 将状态初始化为INSIDE函数,否则我可以在输入ok名称后更改名称。
  3. 输出txt变量
  4. 从不在客户端JS中进行密码测试 - 但我想你只是在玩
  5. &#13;
    &#13;
    <input type="text" id="user" value="">
    <input type="password" id="pass" value="">
    <button type="button" id="btn" onclick="func()">Login</button>
    <h1 id="txt"></h1>
    <script>
      function func() {
        var utxt = document.getElementById("user").value;
        var ptxt = document.getElementById("pass").value;
        var userstate = utxt == "david";
        var passstate = ptxt == "lol123";
        document.getElementById("txt").innerHTML=(userstate && passstate)?"Login successful": "Login not successful";
      }
    </script>
    &#13;
    &#13;
    &#13;

    使用个人资料重新:请勿使用基于客户端的密码验证,除非让您的小妹妹不要进入某个页面。

    &#13;
    &#13;
    <input type="text" id="user" value="">
    <input type="password" id="pass" value="">
    <button type="button" id="btn" onclick="func()">Login</button>
    <h1 id="txt"></h1>
    <script>
      var profiles={ "david":"lol123", "fred":"xdf456"};
      function func() {
        var utxt = document.getElementById("user").value;
        var ptxt = document.getElementById("pass").value;
        var success= profiles[utxt] && profiles[utxt]==ptxt; // the name exists and matches the password
        document.getElementById("txt").innerHTML=(success)?"Login successful": "Login not successful";
      }
    </script>
    &#13;
    &#13;
    &#13;