2011-12-27 74 views
0

我想做以下工作,但無法弄清楚如何將變量與字符串結合起來。我評論了下面我不明白的地方。JQuery結合變量與字符串

謝謝!

$('.mcTransferGroup').each(function() { 
    var mcAdd = $(this).find('#mcAdd'); 
    var mcRemove = $(this).find('#mcRemove'); 
    var mcSelect1 = $(this).find('.mcSelect1'); 
    var mcSelect2 = $(this).find('.mcSelect2'); 

    $(mcAdd).click(function() { 
     // below here 
     $(mcSelect1, 'option:selected').remove().appendTo(mcSelect2); 
    }); 
    $(mcRemove).click(function() { 
     // and here ... 
     $(mcSelect2, 'option:selected').remove().appendTo(mcSelect1); 
    }); 
}); 
+0

爲什麼在事件處理程序中返回? – sje397 2011-12-27 11:31:42

回答

1

嘗試例如爲:

$('option:selected', mcSelect1).remove().appendTo(mcSelect2); 

的情況下應該是第二個參數。

下面是一個例子:http://jsfiddle.net/ZbZx9/

0

使用http://api.jquery.com/find/(對於給定元素中發現元素)和http://api.jquery.com/filter/(過濾現有jQuery的選擇)。

+0

但是,如何將變量與過濾器結合使用?手冊沒有提到它。 – user1002039 2011-12-27 11:36:01

+0

如果mcSelect1是選擇標籤 - 請使用mcSelect1.find('...')。如果它已經是選項集(我不能確定它是什麼,因爲你沒有提供HTML代碼) - 使用mcSelect1.filter('...')。 – oryol 2011-12-27 14:51:54