我有一个表单,我正在尝试使用 jquery validate 插件。我的问题是我无法访问成功回调中的标签。我可以得到label.attr('for')
,但是当我尝试时label.attr('id')
,或者label.html()
,我得到未定义。
这是我的验证电话:
<script type="text/javascript">
$(document).ready(function() {
$('#personal_info').validate({
rules: {
LastName: "required",
Age: "required"
},
messages: {
LastName: "",
Age: ""
},
errorPlacement: function(error, element) {
console.log('element.attr = '+element.attr('name'));
if(element.attr('name') == 'Age') {
console.log('element = '+element.parents().siblings('label').html());
element.parents().siblings('label').removeClass('highlight').addClass('warning');
}
//error.insertBefore(element.parent().children("br"));
},
// specifying a submitHandler prevents the default submit, good for the demo
submitHandler: function() {
document.personal_info.submit();
},
validClass: "checkmark",
// set this class to error-labels to indicate valid fields
success: function(label) {
console.log('the label for = '+label.attr('for'));
console.log('the label id = '+label.attr('id'));
}
});
});
</script>
这是我的一段html:
<label for="Age" id="age18" class="highlight"> Are you at least 18 years of age?</label>
<table id="OnlineAppWizard_InputAge18" border="0">
<tr>
<td>
<input id="Age18_yes" type="radio" name="Age" value="Yes" <?
if ($Age18 == "Yes") {
print "checked";
}
?>/>
<label for="Age18_yes">Yes</label>
</td>
<td>
<input id="Age18_no" type="radio" name="Age" value="No" <?
if ($Age18 == "No") {
print "checked";
} ?>/>
<label for="Age18_no">No</label>
</td>
</tr>
</table>
当我单击其中一个单选按钮时,萤火虫控制台中会出现以下内容:
the label for = Age
the label id = undefined
您可以从 html 片段中看到带有“Age”的“for”属性的标签标签,也有一个“age18”的“id”属性