3 回答

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
請(qǐng)根據(jù)您的結(jié)構(gòu)嘗試以下代碼:
function foo(){
var lList = $(".p1_lSelect");
var rList = $(".p1_rSelect");
bar(lList);
bar(rList);
}
function bar(list){
$(list).find("option:selected").each(function() {
selected = $(this).text();
console.log(selected);
});
//Console log does not print
}

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊
您可以傳遞列表的名稱并將其附加到選擇器,如下所示:
function foo(){
bar("p1_lSelect");
bar("p1_rSelect");
}
function bar(list){
$(`.${list} option:selected`).each(function() {
selected = $(this).text();
console.log(selected);
});
//Console log does not print
}

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用find()方法:
function bar(list) {
list.find("option:selected").each(function() {
console.log($(this).text());
});
});
添加回答
舉報(bào)