2009-12-08 73 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