Jquery - three select lists, deactivate the other two if one is value is selected
I have three drop down select lists, when a value from one of the list is selected, I want the other two to be greyed out and inactive. I have id wrappers around each select list that I'm experimenting o开发者_StackOverflown, but as I am new with jquery I'm not getting very far.
Something like this should work:
$(function() {
var $selects = $('#select1, #select2, #select3');
$selects.change(function() {
// disabled the other two
$selects.not(this).attr('disabled', 'disabled');
});
});
Updated version:
$(function() {
var $selects = $('#select1, #select2, #select3');
$selects.change(function() {
// disable or enable the other two
$selects.not(this).attr('disabled', $(this).val() === '' ? '' : 'disabled');
});
});
I assume you have three, and if any of the three are selected, the other two should disable.
function toggleOthers(x) {
var select = new Array('#wrapper1','#wrapper2','#wrapper3');
for (int i = 0; i < select.length; i++ )
if ($(x).attr('id')!=select[i])
$(x).attr('disabled','disable');
}
HTML:
<select onchange='toggleOthers(this);' id='wrapper1'> etc...
You can do something like this:
$('#wrappers').change(function(){
$('#select1').attr('disabled', true);
$('#select2').attr('disabled', true);
});
Where wrappers
is the id of select element from which when you choose a value the other two get disabled whose id is select1
and select2
respectively eg:
<select id="wrappers">.........
<select id="select1">.........
<select id="select2">.........
Here is working example
if you have the selects with a div wrapper with an id of #wrapper
$('#wrapper select').each(function(i,select){
$(select).change(function(){
$(this).attr('class','enabled').siblings().attr('class','disabled');
})
})
精彩评论