我们将研究实现这一目标的两种方法。使用和不使用jQuery。
1.使用jQuery
您需要将添加KEYUP功能,您的密码和确认密码字段。原因是即使
password字段更改,也应检查文本相等性。感谢@kdjernigan指出
这样,当您在字段中键入内容时,您将知道密码是否相同:
$('#password, #/confirm/i_password').on('keyup', function () { if ($('#password').val() == $('#/confirm/i_password').val()) { $('#message').html('Matching').css('color', 'green'); } else $('#message').html('Not Matching').css('color', 'red');});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>password : <input name="password" id="password" type="password" /></label><br><label>confirm password: <input type="password" name="/confirm/i_password" id="/confirm/i_password" /> <span id='message'></span></label>这是小提琴:http :
//jsfiddle.net/aelor/F6sEv/325/
2.不使用jQuery
我们将在两个字段上使用javascript
的onkeyup事件来达到相同的效果。
var check = function() { if (document.getElementById('password').value == document.getElementById('/confirm/i_password').value) { document.getElementById('message').style.color = 'green'; document.getElementById('message').innerHTML = 'matching'; } else { document.getElementById('message').style.color = 'red'; document.getElementById('message').innerHTML = 'not matching'; }}<label>password : <input name="password" id="password" type="password" onkeyup='check();' /></label><br><label>confirm password: <input type="password" name="/confirm/i_password" id="/confirm/i_password" onkeyup='check();' /> <span id='message'></span></label>这是小提琴:http :
//jsfiddle.net/aelor/F6sEv/324/



