2012-07-13 50 views
0

我已經通過看了一個答案的計算器,幾乎在那裏,但需要一些幫助。根據以前的選擇刪除下拉條目

我有多個下拉列表,其中有相同的選項。例如三個相同的清單與以下內容:

<label for='distlist_1'>Distribution List 1</label> 
<select name='distlist_1'> 
<option value=''>Please Select:</option> 
<option value='1'>All</option> 
<option value='2'>All Managers</option> 
<option value='3'>Leavers</option> 
</select> 

唯一的區別是名稱例如distlist_1,2,3等我可以根據需要添加的ID。

當用戶在第一個下拉列表中選擇一個選項時,我需要將其從所有其他下拉列表中刪除。我找到了一個腳本來做到這一點。

$('option').click(function(){ 
$('option:contains(' + $(this).html() +')').not(this).remove(); 
}); 

但我需要它,這樣,如果用戶決定,「等我不需要這個選項在下拉列表1畢竟」,她拿起別的東西,並選擇在其他下拉菜單再次出現。上面的代碼刪除了選項,然後沒有辦法檢索它們,直到你點擊的時候它們全部消失。

+0

可以隱藏的選項,然後再告訴他們。 – undefined 2012-07-13 11:19:59

+0

我該怎麼做,我卡住了?我發現這個,它工作,但它是很多代碼 - http://forums.asp.net/t/1738132.aspx/1,我需要編輯它的每一組下拉列表。希望得到一個簡潔的jQuery腳本。在我原來的腳本中可能只需要調整,但我不知道如何。 – Richard 2012-07-13 11:46:23

回答

0

最後我用這個,因爲它是簡明扼要......

jQuery(document).ready(function() { 
var selects = $('.mapping') 

selects.change(function() { 
var vals = {}; 
selects.each(function() { 
vals[this.value] = true; 
}).get(); 
selects.not(this).children().not(':selected').not(':first-child').each(function() { 
this.disabled = vals[this.value]; 
}); 
}); 
}); 
0

這其實你想要做什麼......這是基於jQuery 1.7和on()事件粘結劑

$(document).ready(function(){ 
     $('select').on('focus', function() { 
     // on focus save the current selected element so you 
     // can place it back with all the other dropdowns 
     var current_value = $(this).val(); 

     // bind the change event, with removes and adds elements 
     // to the dropdown lists 
     $(this).one('change.tmp', { v : current_value }, function(e) 
     { 
      if ($(this).val() != '') 
      { // do not remove the Please select options 
      $('option[value="' + $(this).val() + '"]') 
       .not($('option[value="' + $(this).val() + '"]', $(this))) 
       .remove(); 
      } 

      if (e.data.v != '') 
      { // do not add the Please select options 
      $('select').not($(this)).append($('option[value="' + e.data.v + '"]', $(this)).clone()); 
      } 
     }); 

     // on blur remove all the eventhandlers again, this is needed 
     // else you don't know what the previously selected item was. 
     $(this).on('blur.tmp', { v : current_value}, function (e) 
     { 
      $(this).off('blur.tmp'); 
      $(this).off('change.tmp'); 
     }); 
     }); 
    }); 

它不會做的是排序按正確的順序一切的唯一的事情。你將不得不在.append函數中考慮你自己的做法。

+0

工作,輝煌!我已經看到如此多的變化,但只需將它放入頁面即可。你有什麼機會可以幫我實施,我可能會分開一堆下跌? IE,我的第一篇文章,Imight有printer1,pnrinters2等,但後來在相同的表格/頁面上,我可能有monitor1,monitor2。正如當我運行這個時,如果我chnage下拉它會影響頁面上的每個元素。 – Richard 2012-07-13 13:46:57

+0

我可以爲每組下拉列表使用一個id,然後在函數的頂部以某種方式實現它? – Richard 2012-07-13 13:48:40

+0

理查德,對於遲到的回覆感到抱歉;)無論如何,你可以很容易地改變它以支持團體...我建議你使用jquery選擇器並改變它...使用某個標記對它們進行分組,會建議跨度並給這個跨度一個id ...然後將選擇器$('select')改爲像這樣$('select',$('#spanIdHere')),那麼你只能選擇指定的選項元素與ID spanIdHere ...乾杯;) – bkwint 2012-07-14 18:11:47

相關問題