2010-08-10 117 views

回答

4

像这样的东西应该工作:

$(function() { 
    var $selects = $('#select1, #select2, #select3'); 
    $selects.change(function() { 
    // disabled the other two 
    $selects.not(this).attr('disabled', 'disabled'); 
    }); 
}); 


更新版本:

$(function() { 
    var $selects = $('#select1, #select2, #select3'); 
    $selects.change(function() { 
    // disable or enable the other two 
    $selects.not(this).attr('disabled', $(this).val() === '' ? '' : 'disabled'); 
    }); 
}); 
+0

这是非常优雅,容易理解。每个下拉列表都有一个“无选择”选项。这里的价值就是“”。如果您希望其他字段在选择“无选择”时再次激活,您将如何编写它? – Toxid 2010-08-10 23:10:11

+0

添加了一个更新版本,该版本应该启用或禁用其他两个版本,具体取决于触发更改事件的select的当前值。感谢你的接纳! – jmar777 2010-08-10 23:22:03

+0

简直太棒了!你有一个太多=但是,应该是.val()==。我认为这是一个错字。 – Toxid 2010-08-10 23:25:50

0

你可以做这样的事情:

$('#wrappers').change(function(){ 
    $('#select1').attr('disabled', true); 
    $('#select2').attr('disabled', true); 
}); 

哪里wrappers是,当你选择一个值另外两个获得残疾id为select1select2例如分别从中选择元素的ID:

<select id="wrappers">......... 
<select id="select1">......... 
<select id="select2">......... 

Here is working example

0

我假设你有三个,如果任何三个是塞莱另外两个应该禁用。

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...

0

如果你有选择用一个div包装带#wrapp的ID Ε

$('#wrapper select').each(function(i,select){ 
    $(select).change(function(){ 
     $(this).attr('class','enabled').siblings().attr('class','disabled'); 
    }) 
}) 
相关问题