我正在为我的网站创建和注册表格,我正在尝试添加一个 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/