[我正在使用jQuery遍历许多Select Option html元素,我一次可以使它工作一次,但是如果将数据存储在jQuery元素中,我将很难访问它。
例如,我有这两个Select html元素
var lList = $(".p1_lSelect");
var rList = $(".p1_rSelect");
我可以通过以下操作获得所需的输出
$(".p1_lSelect option:selected").each(function() {
selected = $(this).text(); //selected == "p1_lSelect_Data"
console.log(selected);
});
$(".p1_rSelect option:selected").each(function() {
selected = $(this).text(); //selected == "p1_rSelect_Data"
console.log(selected);
});
//Console logs .p1_lSelect_Data and .p1_rSelect_Data as expected
我想要的类似于以下内容,以便我可以更改要输入到循环中的列表
function foo(){
var lList = $(".p1_lSelect");
var rList = $(".p1_rSelect");
bar(lList);
bar(rList);
}
function bar(list){
$("list option:selected").each(function() {
selected = $(this).text();
console.log(selected);
});
//Console log does not print
}
我将如何去做?
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
}
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
}
function bar(list) {
list.find("option:selected").each(function() {
console.log($(this).text());
});
});