2009-12-08 63 views
2

我有一个包含默认选项(它有一个ID)的html选择输入。我也有一个JSON对象,看起来像在FBJS中使用json对象更改HTML选择选项

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

我将如何从使用Facebook的JS JSON对象的值并显示替换选择的选项? (FBJS)

+1

我希望我可以给你更多的upvotes - 我几乎FBJS(和普通的老爵士)文盲,我一直在敲我的头这天。 – 2010-02-03 00:02:23

+0

谢谢,fbjs有点令人沮丧,因为遍布互联网的许多简单答案都不能很好地发挥作用。对于下一个项目,我可能只是iframe。 – RobKohr 2010-02-09 16:04:32

回答

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

1

尝试在FBJS维基条目FBJS

,你还需要使用

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

创建选择节点中选择节点所描述的FBJS功能selectNode.removeChild