我试图禁用我的HTML格式的按钮,但我不知道出了什么问题可以帮助我

时间:2018-03-14 19:32:03

标签: javascript html

我正在创建密码更改页面。我想在两个密码不匹配时禁用提交按钮。有些人向我解释,我不知道出了什么问题?

<form role="form">
    <div class="form-group">
        <label>Existing Password</label>
        <input class="form-control" name="password" type="password" placeholder="Existing Password" required/>
    </div>
    <div class="form-group">
        <label>New Password</label>
        <input class="form-control" name="npassword" type="password" placeholder="New Password" id="npassword" onchange='check_pass();' required/>
    </div>
    <div class="form-group">
        <label>Confirm New Password</label>
        <input class="form-control" name="cnpassword" type="password" placeholder="Confirm New Password" id="cnpassword " onchange='check_pass();' required/>
    </div>
    <button name="submit" type="submit" class="btn btn-default" id="submit" disabled>Submit </button>
    <button type="reset" class="btn btn-default">Reset Button</button>
    <script>
        function check_pass() {
            if (document.getElementById('npassword').value == document.getElementById('cnpassword').value) {
                document.getElementById('submit').disabled = false;
            } else {
                document.getElementById('submit').disabled = true;
            }
        }
    </script>
</form>

1 个答案:

答案 0 :(得分:0)

你在cnpassword的id中有一个空格。

&#13;
&#13;
<form role="form">
  <div class="form-group">
    <label>Existing Password</label>
    <input class="form-control" name="password" type="password" placeholder="Existing Password" required/>
  </div>
  <div class="form-group">
    <label>New Password</label>
    <input class="form-control" name="npassword" type="password" placeholder="New Password" id="npassword" onchange='check_pass();' required/>
  </div>
  <div class="form-group">
    <label>Confirm New Password</label>
    <input class="form-control" name="cnpassword" type="password" placeholder="Confirm New Password" id="cnpassword" onchange='check_pass();' required/>
  </div>
  <button name="submit" type="submit" class="btn btn-default" id="submit" disabled>Submit </button>
  <button type="reset" class="btn btn-default">Reset Button</button>
  <script>
    function check_pass() {
      if (document.getElementById('npassword').value == document.getElementById('cnpassword').value) {
        document.getElementById('submit').disabled = false;
      } else {
        document.getElementById('submit').disabled = true;
      }
    }
  </script>
&#13;
&#13;
&#13;