I'm using the jQuery Validate plug-in and need to require a field if a certain radio button is checked. How can I determine if a certain country is selected?
For example, if the Canada radio button is checked, then require this field. This isn't right, but it's something along these lines:
depends: function(element) {
return $("input[name='country'],[value='ca'],:checked")
}
UPDATE:
I'm using @Tatu Ulmanen's code, however I'm receiving the following error after clicking outside of one of the fields that should be required when "Canada" is checked. Any ideas?
province: {
depends: function(element) {
return $("开发者_运维知识库input[name='country'][value='ca']").is(':checked')
}
},
$.validator.methods[method] is undefined [Break On This Error] eval(function(p,a,c,k,e,d){e=function(...x5b|trigger|x21|x23'.split('|'),0,{}))
$("input[name='country'][value='ca']").is(':checked');
You can use the .is()
method:
$("input[name='country'][value='ca']").is(':checked');
$("input[name='country'],[value='ca']").is(':checked');
here's a way:
if ($('#id').is(':checked')) {
alert('button is checked');
}
As well as using .is()
, jQuery 1.6 introduced .prop()
, see
http://api.jquery.com/prop/ for details.
精彩评论