gpt4 book ai didi

javascript - 从 jquery 中的选择选项添加两个不同的数组值(文本和值)

转载 作者:行者123 更新时间:2023-11-29 15:40:12 25 4
gpt4 key购买 nike

我将如何添加另一组数组并将其放在选项的值上。

这是我的部门数组

var dept = ['dept1','dept2','dept3'];



$.each(dept, function(index, value){
$("#filter2").append($("<option>",{
value: value, //--this is where i should put the value of array dept_id
text: value
}));

我想添加我的另一个数组并将其放入每个选项的值中:

var dept_id = ['35', '36', '37'];

最佳答案

fiddle Demo

使用 value: dept_id[index],

var dept = ['dept1', 'dept2', 'dept3'],
dept_id = ['35', '36', '37'],
filter2 = $("#filter2");
$.each(dept, function (index, value) {
filter2.append($("<option>", {
value: dept_id[index],
text: value
}));
});


在 OP 发表评论后更新

fiddle Demo

var dept = ['dept1', 'dept2', 'dept3'],
dept_id = ['35', '36', '37'],
filter2 = $("#filter2");
$.each(dept_id, function (index, value) {
filter2.append($("<option>", {
value: value,
text: 'dept' + ++index
}));
});

关于javascript - 从 jquery 中的选择选项添加两个不同的数组值(文本和值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com