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