我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/来验证允许用户更改其现有设置的表单。表单包含密码字段以及确认密码的字段。为了保持密码不变,我指示用户将其留空(可能是更直观的方式吗?),只有在用户更改密码时才会显示确认密码字段。
效果很好,但有两个问题。首先,如果您在密码字段中输入内容并按Tab键,则不会转到确认密码字段,而是转到下一个字段。其次,如果您在密码字段中输入内容并按Enter键,则会在检查确认密码之前提交表单。出于某种原因,第二个缺陷并不表现在使用jsfiddle demo http://jsfiddle.net/4htKu/2/,而是在第二个相同的演示http://tapmeister.com/test/validatePassword.html上表现出来。下面的代码与前面提到的两个演示相同。我认为问题与隐藏的confirm_password密码有关,但我不确定。我需要做些什么来解决这两个问题?谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>jQuery validation plug-in - main demo</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#confirm_password").parent().hide();
$("#password").val('').
blur(function() {
$t=$(this);
if($.trim($t.val())!="" && !$t.hasClass('error')) {$("#confirm_password").parent().show();}
else if($.trim($t.val())=="") {$("#confirm_password").val('').parent().hide();}
});
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: {required: true,minlength: 2},
lastname: {required: true,minlength: 2},
username: {required: true,minlength: 2},
password: {required: true,minlength: 2},
confirm_password: {required: true,minlength: 2, equalTo: "#password"}
},
messages: {},
submitHandler: function(form) {alert("submitted");}
});
});
</script>
</head>
<body>
<form class="cmxform" id="signupForm" method="get" action="">
<fieldset>
<legend>Validating a complete form</legend>
<p>
<label for="firstname">Firstname*</label>
<input id="firstname" name="firstname" />
</p>
<p>
<label for="lastname">Lastname*</label>
<input id="lastname" name="lastname" />
</p>
<p>
<label for="username">Username*</label>
<input id="username" name="username" />
</p>
<p>
<label for="password">Password (Leave blank to remain unchanged)</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password" />
</p>
<p>
<label for="other">Other Non-required</label>
<input id="other" name="other" />
</p>
<p>
<input class="submit" type="submit" value="Submit" />
</p>
</fieldset>
</form>
</body>
</html>
答案 0 :(得分:3)
使用.blur
和.keydown
来检测值是否为空,而不是使用settimeout
。如果它不是空白,则显示它,否则隐藏它。
$("#password").keydown(function() {
var self = this, $self = $(this);
setTimeout(function(){
if ( $.trim(self.value) != "" && !$self.is(".error") ) {
$("#confirm_password").parent().show();
return;
}
$("#confirm_password").val('').parent().hide();
},0);
});
至于按下输入,是不是应该做什么?提交表格?
使用.blur
的原因不起作用是在显示隐藏字段之前显示到下一个字段。此代码修复了该问题。
答案 1 :(得分:0)
为什么不通过复选框询问用户是否要更改密码?如果选中,则显示密码字段,如果不允许提交表单。
其他HTML:
<p>
<label for="passwordChangeYes">Check if you want to change your password</label>
<input type="checkbox" name="passwordChangeYes" id="passwordChangeYes" value="Yes" />
</p>
额外的JS:
$("#password").hide();
$('#passwordChangeYes').one("click", function () {
if ($('#passwordChangeYes').is(':checked')) {
$("#password").fadeIn(400);
}
});
我不确定如何在复选框点击中从验证部分删除或添加所需的部分。但也许这是一个起点?