2013-04-25 72 views
4

如何在select中禁用多個選項,以防在其他選擇中選擇確切選項。我發現回答問題相似開採這個環節上的問題: http://jsfiddle.net/dZqEu/根據其他<select>選項禁用<select>選項

jQuery代碼看起來未來:

$(this).siblings('select').children('option').each(function() { 
    if ($(this).val() === value) { 
     $(this).attr('disabled', true).siblings().removeAttr('disabled'); 
    } 

的問題是,這僅適用於2種選擇。我需要總共3個選擇,它應該禁用選擇相同值的可能性。預先

http://jsfiddle.net/dZqEu/968/

TNX:

該代碼將不用於3個選擇工作

當SELECT1被設置爲1,選擇2被設置爲2,則i可以在select3中禁用值1 & 2。它僅禁止值2選擇3 ...

+0

你的代碼被禁止用於*所有選擇所有選擇中選擇的值,*。你的問題到底是什麼? – Matt 2013-04-25 16:43:22

+0

看看3選擇的例子。當select1設置爲1時,選擇2設置爲2,我不能禁用select3中的值1和2。它僅在選擇3時禁用值2 ... – 2013-04-25 16:46:47

+0

您應該在您的小提琴中啓用jQuery。 – tymeJV 2013-04-25 16:47:48

回答

7

試試這個: - http://jsfiddle.net/Z2yaG/

使用對焦穫取prev值並刪除禁用的值。 在你的html中,我爲默認選項增加了值-1。

<option value="-1">No Match</option> 

    var prev = -1; 
$("select").change(function() { 
    if ($(this).val() > -1) { 
     $("select").not(this).find("option[value=" + $(this).val() + "]").attr('disabled', 'disabled'); 
    } 
    $("select").not(this).find("option[value=" + previous + "]").removeAttr('disabled'); 
}).focus(function() { 
    previous = $(this).val(); 
}); 
+0

jQuery版本在http://jsfiddle.net/dZqEu/968/是1.4.4 – hjpotter92 2013-04-25 17:06:06

+0

更新小提琴鏈接與jquery1.4.4 ...感謝指出..我注意到。 – PSL 2013-04-25 17:09:15

+0

免責聲明:我改進了@tymejv回答 – PSL 2013-04-25 17:33:08

2

這似乎工作:http://jsfiddle.net/QLn9b/1/

它是利用原來的代碼:

$("select").change(function() { 
    $("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true); 
}); 
+0

這是我需要的,但代碼應該刪除屬性禁用,當它不再被選中。 .. – 2013-04-25 16:58:53

+0

+1,因爲我的解決方案是您的方法的改進版本。 – PSL 2013-04-25 19:57:08

+0

不錯的完成它!今天得到了其他東西包裝回來:\ – tymeJV 2013-04-25 19:58:13

0

試試這個。

$('select').change(function() 
{  
    var value = $(this).val(); 
    $('option').removeAttr("disabled"); 
    $('select').each(function() 
    { 
     $("option[value=" + $(this).val() + "]").attr("disabled","disabled"); 
    }); 
}); 
1

我認爲這是你在找什麼。

$('select').change(function() { 

    var select_elements = $('select') 
    $.each(select_elements, function(i,elem){ 
    var sel_values = []; 

    // Selecting and Iterating on Sibling Selects   
    var siblis = $(elem).siblings('select') 
    $.each(siblis, function(i, sibli){ 
     if($(sibli).val()!='No Match'){ 
      sel_values.push($(sibli).val()); 
     } 
    }); 

    // Iterating on Options of Select 
    $(this).children('option').each(function() { 
     $(this).removeAttr('disabled'); 

     if($.inArray($(this).val(), sel_values) !== -1){ 
      $(this).attr('disabled', true); 
     } 
    });   

    });  
}); 

如果所有選擇具有從1至6的值,

If select1 value  --> 1, 
and select2 value  --> 3 (1 is disabled) 
then in select3 value --> 5 (1,3 are disabled) 

選擇選擇3後,

In select1 --> 1 (3,5 disabled) 
In select2 --> 3 (1,5 disabled) 
In select3 --> 5 (1,3 disabled) 

這裏是的jsfiddle:http://jsfiddle.net/prem_nagalla1/rY4n3/

希望它能幫助: )

1

這是一種方法。當任何下拉改變時,計算所有選定的值(第一個除外)。如果其他框的選定值出現在值列表中,請禁用相應的選項。

$('select').change(function() { 
    var values = [], 
    others = $(this).siblings('select'), 
    all = others.andSelf(); 

    all.each(function() { 
     if (this.selectedIndex > 0) { 
      values.push(this.options[this.selectedIndex].value); 
     } 
    }); 

    others.each(function() { 
     for (var i = 0; i < this.options.length; ++i) { 
      this.options[i].disabled = $.inArray(this.options[i].value, values) !== -1; 
     } 
    }); 
}); 

Demo

相關問題