2

我有一个带有默认选项集的 html 选择输入(它有一个 ID)。我也有一个看起来像的 json 对象

var replacement_options = {'value 1':'display 1', 'value 2':'display 2' ....

如何使用 Facebook JS 将选择中的选项替换为 json 对象中的值和显示?(FBJS)

4

2 回答 2

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);
            }
    }

于 2009-12-08T22:35:52.343 回答
1

尝试使用selectNode.removeChildFBJS wiki 条目FBJS中描述的 FBJS 函数

您还需要使用

var option = document.createElement('option');
selectNode.appendChild(option);

在选择节点内创建选项节点

于 2009-12-08T21:22:37.577 回答