我希望两个值都存在。或者两个值都为空白为VALID 如何更改此演示以允许两个INPUT字段在空白时有效?
的链接<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang='en' lang='en'>
<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://www.tanela.com/testbin/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform").validate({
groups: {
username: "fname lname"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "fname"
|| element.attr("name") == "lname")
error.insertAfter("#lastname");
else
error.insertAfter(element);
},
debug:true
})
});
</script>
</head>
<body>
<form id="myform" action="">
<div>
<label>Your Name</label>
<input name="fname" class="required" value="Pete" />
<input name="lname" id="lastname" class="required" />
<br/>
<input type="submit" value="Submit"/>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
你可以这样做。看看这个fiddle
var firstNameElem = $('[name=fname]'),
lastNameElem = $('[name=lname]');
$('[name=fname],[name=lname]').keyup(function(){
if(firstNameElem.val() != "" || lastNameElem.val() != ""){
firstNameElem.addClass('required');
lastNameElem.addClass('required');
}else{
firstNameElem.removeClass('required');
lastNameElem.removeClass('required');
}
});