I'm searching for a jQuery plugin that does this.
for example:
<label><input type="checkbox" depends_on="foo=5" name="boo" ... /> Check </label>
<select name="foo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
so the checkbox would only be enabled if the "5" option is selected below.
or
<select name="foo" depends_on="boo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
<label><input type="checkbox" name="boo" ... /> Check </label>
In this case the select should only enabled if "boo" is checked.
I managed to come up with a code that does something like this:
$("*[depends_on]").each(function(){
var source = $(this);
var dep = $(this).attr('depends_on');
var target = dep.split("=");
$("*[name='"+target[0]+"']:not(:hidden)").change(function(){
if($(this).attr('checked')) {
source.removeClass('disabled');
source.removeAttr('disabled');
}
else{
source.attr('disabled', 'disabled');
source.addClass('disabled');
}
}).change();
});
seems to work ok for selects that depend on radios, but I want to implement this for any type of input or combination (or at least most of them), without having to write separate code for each type of input.
anyway, is anyone aware of such a plugin? or maybe have suggestions to my code above? :)
I've come up with this code :
And for the HTML format :
So basically, all you have to do is to have a "target" attribute on an input, which should match the "name" attribute of the dropdown that you want to bind to. Set the value of the input too, which should select the value of its corresponding dropdown when it's checked.
jQuery disable button (NOT Submit) until field validates + Validation Plugin
jQuery enable/disable show/hide button w/ SELECT options. Get remaining option values
jQuery disable SELECT options based on Radio selected (Need support for all browsers)
Here you go :-)
http://jsfiddle.net/balupton/cxcmf/
For an example:
Javascript:
HTML: