javascript - 比较两个密码的JS脚本不起作用
问题描述
我正在为我的网站创建和注册表单,我正在尝试添加一个 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>
在 hmtl 代码下面我放置了这个 JS 脚本:
<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>
有人可以帮我让它工作吗?
解决方案
使用==
or===
比较两个变量和=
赋值:
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
语句
推荐阅读
- python - 列表功能困难
- mongodb - 结果中的父子反转
- asp.net-core - SwaggerUI 中是否支持多个 oauth2 身份验证登录提供程序?
- maven - mulesoft maven 测试结果未公布
- javascript - React setState() 在多个智能组件中无法正常工作
- php - 在 Laravel 中查看如何对 URL 进行 json_decode
- http - HttpContext.Current.Request.Browser.MajorVersion 将 IE11 显示为 IE7
- html - 无法垂直居中 Bootstrap 3 媒体
- c++ - #SQLite This query statement does not work as I thought. What should I do
- android - Progress dialog in Multipart Upload Request