2009-10-21 37 views
1

我是新來的Ajax,和我只是熱愛它。真的很好。麻煩與阿賈克斯(創建<select>動態

但我stucked在這個<select>東西,有啥問題? 我有一個js函數它允許用戶動態註冊一個新產品。 因此,當他在產品領域時,點擊一個按鈕,只需創建一個FORM,他可以在其中添加一個新產品。 ,我得到了這個字段CITY

所以,我不需要在我的mySql表上生成所有城市的<select>

林做的是訪問數據庫和歌廳城市數量的第一件事情,我覺得我做這個迷惑所以......

cities = document.createElement('select'); 
cities.setAttribute('class','coolButton'); 
Cities = new Array(); 
ajax = new Ajax(); 
ajax.doGet('pegacidade.php?act=1',GiveCitiesQty); 

這一切正常,我得到城市的數量。

GiveCitiesQty = function (str) { 
    ajax.doGet('pegacidade.php?act=2&id='+j,PegaNomeCidade); 
    //Cities[i] = document.createElement('option'); 
    //Cities[i].setAttribute('value',i); 
    //Cities[i].appendChild(document.createTextNode((2009+i).toString())); 
    //cities.appendChild(Cidades[i]); 
} 

這是我stucked,我不知道如何動態創建此<select>

任何消耗? (現在我試圖不使用任何工具包,爲真正的學習)

謝謝,賈米菲。

回答

0

解析結果,並有一個對象數組後,我這樣做。然後我將這個添加到我的div中。

var rootElem = document.createElement('select'); 
var optionElem; 
for (var i = 0; i < toolsData.length; i++) { 
    optionElem = document.createElement('option'); 
    rootElem.appendChild(optionElem); 
    optionElem.value = toolsData[i].Name; 
    optionElem.appendChild(document.createTextNode(toolsData[i].Name)); 
}