javascript - dwr addoptions 与关联数组

标签 javascript dwr

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

 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/

相关文章:

javascript - 当子组件的 onClick 被触发时,触发向父组件注册的 onKeyUp 事件

javascript - 将 Web 应用程序从 ExtJS 3 和 DWR 更新到 ExtJS 5

ajax - 如何对 gwt-rpc 调用进行分组?

ajax - setInterval 和轮询之间的区别?

java - DWR 传递字符串

javascript - PayPal 关联结账点击事件

javascript - 带 rails 的自定义国家/州下拉菜单

java - 如何在服务器响应上写入文件对象而不在服务器上保存文件?

javascript - 在 AngularJS 中读取非 JSON 文件

javascript - 如何防止默认和禁用 href