2010-08-10 59 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'); 
    }) 
}) 
相關問題