我有一个带有默认选项集的html选择输入(它有一个ID)。我还有一个看起来像
的json对象 var replacement_options = {'value 1':'display 1', 'value 2':'display 2' ....
如何使用Facebook JS将json对象中的值和显示替换为select中的选项? (FBJS)
答案 0 :(得分:2)
将一些东西拼凑在一起之后,我就能够创建一个函数来实现这一点。
//accepts a object for options {value1:display1, value2:display2...
function updateSelectOptionsWithJSON(element_id, options, 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(node);
}
//you can remove these next 4 lines and the last two parameters of this function
//if you just want options to come from the secton parameter
var node = document.createElement('option');
node.setTextValue(first_display);
node.setValue(first_value);
choiceList.appendChild(node);
for(key in options)
{
var node = document.createElement('option');
node.setTextValue(options[key]);
node.setValue(key);
choiceList.appendChild(node);
}
}
//accepts a object for options {value1:display1, value2:display2...
function updateSelectOptionsWithJSON(element_id, options, 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(node);
}
//you can remove these next 4 lines and the last two parameters of this function
//if you just want options to come from the secton parameter
var node = document.createElement('option');
node.setTextValue(first_display);
node.setValue(first_value);
choiceList.appendChild(node);
for(key in options)
{
var node = document.createElement('option');
node.setTextValue(options[key]);
node.setValue(key);
choiceList.appendChild(node);
}
}
答案 1 :(得分:1)
尝试使用FBJS wiki条目FBJS中描述的FBJS函数selectNode.removeChild
您还需要使用
var option = document.createElement('option');
selectNode.appendChild(option);
在选择节点
中创建选项节点