gpt4 book ai didi

javascript - dwr addoptions 与关联数组

转载 作者:行者123 更新时间:2023-12-03 10:14:35 25 4
gpt4 key购买 nike

我有一个关联数组。我希望能够使用关联数组键作为选项值,使用数组键作为选项元素的文本,将选项添加到下拉列表中。

 var associativeArray = new Array();
associativeArray['city'] = "Portland";
associativeArray['state'] = "Oregon";
associativeArray['country'] = "United States";
dwr.util.addOptions(makeId3.id,associativeArray);

执行上述操作会创建文本和值如下的选项元素:

<option value="city,Portland">city,Portland</option>

但是我期待以下内容:

<option value="city">Portland</option>

如何使用 dwr 和关联数组实现上述目标。有人可以建议吗?

最佳答案

使用对象而不是数组:

var associativeArray = {
city: "Portland",
state: "Oregon",
country: "United States"
};
dwr.util.addOptions(makeId3.id, associativeArray, false);

或者,如果您想动态添加属性,请使用:

var associativeArray = {};
associativeArray['city'] = "Portland";
associativeArray['state'] = "Oregon";
associativeArray['country'] = "United States";

请参阅documentation 。它对待数组参数的方式与对待对象的方式不同。

关于javascript - dwr addoptions 与关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29943609/

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