How to check if a select value equals a certain option using jQuery Validate?
I'm using jQuery Validate for a form. I need to add some functionality so an additional field is required if a specific option is selected from a select/dro开发者_开发百科p-down menu.
Here's the example they provide on the jQuery website to require a certrain field if an option is checked in a checkbox (http://docs.jquery.com/Plugins/Validation/validate#options):
$(".selector").validate({
rules: {
contact: {
required: true,
email: {
depends: function(element) {
return $("#contactform_email:checked")
}
}
}
}
})
Here's what I'm doing, but it returns an error. Basically, if Football is chosen as a Favorite Sport from a drop down, then position is a required field.
$form.validate( {
rules: {
favoriteSport: {
required: true
},
position: {
depends: function(element) {
return $('#favoriteSport[value="football"]');
}
}
}
});
try:
$('#favoriteSport > option[value=football]:selected')
If you're using ASP.Net or some other language that ruins the IDs of your controls, and you're looking for a way to have specific text selected, you can do this:
$('[id$=NormalControlID] :selected').text("SomeValue");
精彩评论