Wil*_* L. 5 jquery jquery-validate
我正在使用jQuery Validation来验证表单,我想要做的就是这样......我想要设置它,如果选中复选框,那么编辑框的验证方式也不同,例如.
如果未选中该复选框,则应如何验证:
weight: { required: true, max: 50 }
Run Code Online (Sandbox Code Playgroud)
如果选中它,应该如何验证它.
weight: { required: true, max: 100 }
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
只要单击复选框,就可以使用Validate插件的内置rules('add')方法动态更改规则.
jQuery:
$(document).ready(function () {
// initialize the plugin
$('#myform').validate({
// other options,
rules: {
// other rules,
weight: {
required: true,
max: 50 // initial value on load
}
}
});
// change the rule on checkbox and update displayed message dynamically
$('#check').on('change', function () {
if ($(this).is(':checked')) {
$('#weight').rules('add', {
max: 100
});
} else {
$('#weight').rules('add', {
max: 50
});
};
$('#weight.error').each(function () {
$(this).valid();
});
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<form id="myform">
<input type="checkbox" id="check" />
<input type="text" id="weight" name="weight" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
工作演示:http://jsfiddle.net/3hGxS/