用jQuery写一个 新密码 和确认新密码
时间: 2024-03-24 15:38:33 浏览: 17
假设你有两个input元素,一个是新密码的输入框,一个是确认新密码的输入框,你可以使用jQuery来监听这两个输入框的值,并进行比较。以下是一个示例代码:
HTML代码:
```
<label for="new-password">新密码:</label>
<input type="password" id="new-password">
<label for="confirm-password">确认新密码:</label>
<input type="password" id="confirm-password">
```
jQuery代码:
```
$(document).ready(function() {
$('#confirm-password').on('keyup', function() {
var newPassword = $('#new-password').val();
var confirmNewPassword = $(this).val();
if (newPassword === confirmNewPassword) {
$(this).removeClass('error');
// 如果两个密码相等,移除错误提示
} else {
$(this).addClass('error');
// 如果两个密码不相等,添加错误提示
}
});
});
```
这段代码会监听确认新密码输入框的keyup事件,当用户松开键盘时,获取新密码输入框和确认新密码输入框的值,并进行比较。如果两个密码相等,就移除错误提示;如果不相等,就添加错误提示(这里用了一个CSS类名error来表示错误)。你可以根据自己的需要修改这段代码。