我有一个带有默认选项集的 html 选择输入(它有一个 ID)。我还有一个 json 对象,看起来像
var replacement_options = {'值 1':'显示 1', '值 2':'显示 2' ....
如何使用 Facebook JS 将选择中的选项替换为 json 对象中的值和显示内容? (FBJS)
最佳答案
将一些东西拼凑在一起后,我能够创建一个函数来执行此操作。
//接受选项对象 {value1:display1, value2:display2...
函数 updateSelectOptionsWithJSON(element_id、选项、first_display、first_value)
{
var choiceList = document.getElementById(element_id);
for(var count = choiceList.getOptions().length - 1; count > -1; count--)
{
var node = choiceList.getOptions()[count];
choiceList.removeChild(节点);
}
//您可以删除接下来的 4 行以及该函数的最后两个参数
//如果您只希望选项来自 secton 参数
var node = document.createElement('option');
节点.setTextValue(first_display);
节点.setValue(first_value);
choiceList.appendChild(节点);
for(键入选项)
{
var node = document.createElement('option');
节点.setTextValue(选项[键]);
节点.setValue(key);
choiceList.appendChild(节点);
}
}
关于javascript - 使用 FBJS 中的 json 对象更改 HTML SELECT OPTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1869884/