正如标题所说,似乎jQuery验证无法识别已经检查过单选按钮,除非它实际被点击。
意思是,如果您点击另一个元素并且此次点击将收音机设置为已选中,则验证无法识别。
Here's a fiddle to show simple example - 点击提交,然后点击单选按钮,一切正常,错误就消失了。然后刷新,点击提交,然后点击"点击我"文字,你会看到收音机被检查,但错误不会消失。
有解决方法吗?
以下是代码:
<form>
<div class="click">Click me</div>
<input type="radio" name="options" class="radioB">
<input type="submit" value="submit" class="submit">
</form>
<div id="error--holder"></div>
$('.click').on('click', function (e) {
$('.radioB').prop('checked', true)
});
$("form").validate({
rules: {
options: {
required: true
}
},
messages: {
options: "Whoops! Please indicate who.",
},
errorPlacement: function(error, element) {
$('#error--holder').html(error.text()+' <a href=#>Go fix it...</a>').fadeIn();
},
success: function(error) {
$('#error--holder').fadeOut();
},
submitHandler: function (form) {
return false; // stop
}
});
答案 0 :(得分:3)
您可以手动验证字段
$('.click').on('click', function (e) {
$('.radioB').prop('checked', true).valid()
});
演示:Fiddle
答案 1 :(得分:3)
在点击事件
上验证表单$('.click').on('click', function (e) {
$('.radioB').prop('checked', true);
$("form").validate();
});