试试这个:http: //jsfiddle.net/aaK9E/51/
var showPass = false;
$('#c').attr('disabled', 'disabled'); // disabled on doc ready
$('#c').change(function () {
showPass = ($('#c:checked').length > 0);
if (showPass) {
$('#p').hide();
$('#t').show();
} else {
$('#t').hide();
$('#p').show();
}
});
$('#p').keypress(function () { // use keypress instead of change
$('#c').removeAttr('disabled'); // remove the attr 'disabled'
if (!showPass) $('#t').val($('#p').val());
});
$('#t').change(function () {
if (showPass) $('#p').val($('#t').val());
});
backspace is done
如果密码字段没有任何值,您可以更改为此:
$('#p').keyup(function () {
$('#c').removeAttr('disabled');
if (!showPass) {
$('#t').val($('#p').val());
}
if ($(this).val() == '') { // this will check the val if there is not
$('#c').attr('disabled', 'disabled'); it will disable it again.
}
});
如果使用lates jquery:
$('#p').on('keyup', function () { // this '.on()' requires latest jquery
$('#c').removeAttr('disabled');
if (!showPass) {
$('#t').val($('#p').val());
}
if ($(this).val() == '') { // this will check the val if there is not
$('#c').attr('disabled', 'disabled'); it will disable it again.
}
});
如果需要try to use latest jquery