2012-08-15 76 views
0

你好,我想使用JavaScript創建動態下拉列表,但它不工作..動態下拉菜單使用JavaScript創建

我的PHP代碼:

<input type="button" onclick="javascript:addRow();" value="Add Row" name="addRow"/> 

我的JS代碼:

// This function is used to create dynamic form element 
function addRow() { 
    var opt = document.createElement("option"); 
    element = document.createElement("select"); 
    element.setAttribute('id', 'focus'); 
    element.options.add(opt); 
} 

PS:它沒有給出任何js控制檯錯誤。

回答

3

你不要的元素添加到人體,因此不會出現

// This function is used to create dynamic form element 
function addRow() { 
    var opt = document.createElement("option"); 
    element = document.createElement("select"); 
    element.setAttribute('id', 'focus'); 
    element.options.add(opt); 
    document.body.appendChild(element); 
} 
+0

+1 - 打我給它。 – Fenton 2012-08-15 10:49:07

+0

它應該可能被添加到窗體,但嘿.. :) – 2012-08-15 10:51:16

+0

thanx fr快速幫助..雅我絕對張貼代碼的一部分..其咬更長.. – 2012-08-15 11:02:34