我對你的代碼做了一些修改,你可以看到它們here。它在開始時也不適合我,但現在它按預期工作,我認爲。如果你有任何問題,只是評論:)
這裏,我們去:
重新聲明$selects
陣列,一旦你刪除的元素。對元素的引用仍然在數組中,所以當你稍後計算選定的索引時,它仍然會在那裏!
select.next('button').click(function(event) {
event.preventDefault();
select.remove();
$(this).remove();
$selects = $('select'); // <-- redeclare selects here, one is gone!
});
在選擇單擊處理程序,使用nth-child
代替eq
。前者將選擇所有option
,即他們父母的子女。而eq
只會從匹配元素集合中選擇第i
個元素(即它只會返回一個元素)。請注意,nth-child
的索引是基於一個的。
// Remove them from this dropdown
$.each(selectedIndexes, function(i, index) {
// use n-th child here to get the child of each select.
// eq only selects the element from the matched set
$thisSelect.find('option:nth-child('+(index+1)+')').attr("disabled","disabled");
});
我在所有三個下拉菜單中選擇'1'成功。 – Amarghosh 2010-07-09 05:52:26
@Amarghosh你能爲我詳細闡述一下嗎?您是否禁用了JavaScript?更好的是,發佈一個答案,如果它有用,我會投票。乾杯。 – alex 2010-07-09 05:55:04
@alex:刪除'console.log'一行,它在jsbin中拋出一個錯誤,並打破了javascript。我猜Amarghosh有同樣的問題。 – jAndy 2010-07-09 05:58:40