javascript - 比较两个密码的 JS 脚本不起作用

标签 javascript html bootstrap-4

我正在为我的网站创建和注册表格,我正在尝试添加一个 JS 脚本,它比较两个密码输入(密码和重复密码),然后显示一条消息“密码不相同”。这是我的代码:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header" style="margin-top: 5px; margin-bottom: 5px">

        <h5 class="modal-title" id="exampleModalLabel" style="text-decoration: underline;text-align: center; font-size: 25px; line-height: 20px">Zarejestruj sie ! </h5>

      </div>
      <div class="modal-body" style="margin-top: 10px">
        <form>
          <div class="form-group">
            <form>
              <div class="form-group">
                <label>New Login</label>
                <input type="text" class="form-control" placeholder="your login">
              </div>
              <div class="form-group">
                <label>New password</label>
                <input type="password" class="form-control" id="pas1" placeholder="password">
              </div>
              <div class="form-group">
                <label>Repeat password</label>
                <input type="password" class="form-control" id="pas2" placeholder="password">
              </div>
              <p id="alert"></p>
            </form>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
            <button type="submit" class="btn btn-primary" onclick="Funkcja()">Zarejestruj konto</button>
          </div>

      </div>
    </div>
  </div>

在我放置这个 JS 脚本的 hmtl 代码下面:

<script>
function Chceck() {
    var x, y, text;

    // Get the value of the input field with id="numb"
    x = document.getElementById("pas1").value;
    y = document.getElementById("pas2").value;

    // If x is Not a Number or less than one or greater than 10
    if (x = y) {
    text= "";

    } else {
        text = "Hasla nie sa takie same";
    }
  document.getElementById("alert").innerHTML = text;
}
</script>

有人可以帮我让它工作吗?

最佳答案

使用=====比较两个变量,使用=赋值:

if (x == y) { // change this line to be 'x == y' from 'x = y' 
    text= "";
} else {
    text = "Hasla nie sa takie same";
}

您可以认为 == 不如 ===

更“严格”

例如 1 == true 是一个 true 语句,但是 1 === true 是一个 false声明

关于javascript - 比较两个密码的 JS 脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51285815/

相关文章:

javascript - 将此 javascript 函数转换为 jquery

javascript - ionic 后退按钮未显示在选项卡页面中且动画丢失

javascript - 在 Polymer 中获取文本字段的值

javascript - 动画不透明度在 jquery 中不能正常工作

javascript - Jquery - 获取最接近的div并显示

html - 移动标题没有背景颜色

javascript - 下拉菜单 popper.js

javascript - 如何在传递给另一个组件的函数中获取 useState 变量的值

javascript - 除了具有 float 和中心的 div 类之外布局 div

angular - 在 Angular 4 中无限调用方法调用的异步管道