2014-04-11 20 views
0

我有一個JavaScript函數,它向表中添加一個新行。它絕對很好地添加數據類型,如輸入(文本),文本框和廣播。它也可以使用將工作select元素添加到現有表中

document.createElement('select');

但是,當我嘗試添加元素時,觸發JS函數的按鈕什麼也不做,我假設這意味着有錯誤,但我沒有得到任何問題的跡象。我確信我錯過了一些簡單的東西,但是它一直困擾着我幾個小時,這讓我瘋狂了!

該函數的主體是好的,因爲任何其他數據類型創建並相應地將它自己添加到表格單元格。該工作功能中正確JS的例子:

// inner left cell - text input 
    var cellTwo = row.insertCell(1); 
    var el = document.createElement('input'); 
    el.type = 'text'; 
    el.name = 'col' + rowNumber; 
    el.id = 'col' + rowNumber; 
    cellTwo.appendChild(el); 

嘗試和創建工作的選項中選擇元素的代碼:(NB如果我註釋掉選項的一部分,只是創建一個空的選擇元素這個工程,所以必須將錯誤的地方在福爾循環內,必須僅僅是我誤會的結果如何動態創建選項:

// inner right cell - select box 
    var cellThree = row.insertCell(2); 
    //create select element 
    var dropDown = document.createElement('select'); 
    //give select element id and name 
    dropDown.id = 'fieldtypecol' + rowNumber; 
    dropDown.name = 'fieldtypecol' + rowNumber; 

    //declare array values to put in option elements 
    var optionValue[] = new Array('varchar(255)', 'int', 'date', 'float(53)'); 
    var optionText[] = new Array('String', 'Integer', 'Date', 'Float'); 

    //declare option element holder 
    var option; 

    //loop through creating and adding values to options 
    for (var i = 0; i < 4; i++) 
    { 
    option = document.createElement('option'); 
    option.value = optionValue[i]; 
    option.text = optionText[i]; 
    //append current option before loop restarts 
    dropDown.appendChild(option); 
    } 
    //append select element to new table cell 
    cellThree.appendChild(dropDown); 

任何幫助將不勝感激,謝謝

+0

'optionValue []'是一個語法錯誤,您不要在javascript中聲明變量名後加'[]'。看看你的JavaScript控制檯,看看語法和類似的錯誤 –

回答

0

它出現在你的代碼行;

var optionValue[] = new Array('varchar(255)', 'int', 'date', 'float(53)'); 
var optionText[] = new Array('String', 'Integer', 'Date', 'Float'); 

應該是;

var optionValue = new Array('varchar(255)', 'int', 'date', 'float(53)'); 
var optionText = new Array('String', 'Integer', 'Date', 'Float'); 

類型在JS中是動態的,我以爲這些錯誤會出現在JS控制檯中。