2013-03-20 68 views
0

我已經創建了動態表,它在單擊按鈕後在最後添加行。在一列中我有dropdown list,我想包含數據庫值。

這是我如何創建我的表:將多個下拉列表添加到填充了數據庫值的表中

<script type="text/javascript"> 
    function createRow() { 
     var row = document.createElement('tr'); // create row node 
     var col = document.createElement('td'); // create column node 
     var col2 = document.createElement('td'); // create secondcolumn node 
     var col3 = document.createElement('td'); // create next node 
     var col4 = document.createElement('td'); // create next column node 
     var col5 = document.createElement('td'); // create next column node 
     row.appendChild(col); // append first column to row 
     row.appendChild(col2); // append second column to row 
     row.appendChild(col3); // append next column to row 
     row.appendChild(col4); // append next column to row 
     row.appendChild(col5); // append next column to row 
     col.innerHTML = '<select name="search-alias-0017" id="hasla" title="Search in">' + 
      '<option value="aps" selected="selected">--Wybierz z listy--</option> ' + 
      '<option value="stripbooks">opcja</option>' + 
      '<option value="popular">Haslo2</option>' + 
      '</select>'; 

     c//rest columns 
     var table = document.getElementById("tableToModify"); // find table to append to 
     table.appendChild(row); // append row to table 
    } 
</script> 

現在我有drop down list兩種選擇。我修改了這個,並從我的數據庫中加載了一些變量,我希望這些值被放到<option>標記中。其實我的長相JS



<script type="text/javascript"> 
     function createRow() { 
<? $q = "select name from vulnerability"; 
     $result = $db - > query($q) - > fetchall(); ?> 
     var options = ''; 
     options += '<select name="search-alias-0017" id="hasla" title="Search in">'; 
     for (var i = 0; i < <? echo $result(sizeof($result); ?> ; i++) { 
      options += '<option> <? echo $result[i]['name ']; ?> < /option>'; 
    } 
    options+='</select > '; 
      var row = document.createElement('tr'); // create row node 
      var col = document.createElement('td'); // create column node 
      var col2 = document.createElement('td'); // create secondcolumn node 
      var col3 = document.createElement('td'); // create next node 
      var col4 = document.createElement('td'); // create next column node 
      var col5 = document.createElement('td'); // create next column node 
      row.appendChild(col); // append first column to row 
      row.appendChild(col2); // append second column to row 
      row.appendChild(col3); // append next column to row 
      row.appendChild(col4); // append next column to row 
      row.appendChild(col5); // append next column to row 
      col.innerHTML = options; 

      c//rest columns 
      var table = document.getElementById("tableToModify"); // find table to append to 
      table.appendChild(row); // append row to table 
     } 
    </script> 

但不知何故,我的代碼does not工作...有人可以給我一個提示我究竟做錯了什麼?

回答

0

首先嚐試執行

col.innerHTML = '<select name="search-alias-0017" id="hasla" title="Search in">' + 
      '<option value="aps" selected="selected">--Wybierz z listy--</option> ' + 
      '<option value="stripbooks">opcja</option>' + 
      '<option value="popular">Haslo2</option>' + 
      '</select>'; 

然後

row.appendChild(col); 

進行更改col如果追加後做也不會做任何改變。當你追加時,appendChild()函數會複製它的輸入參數,所以在這之後,你無法按照你嘗試的方式訪問它。

+0

這不是我想是因爲第一個劇本我張貼的作品,因爲它應該的問題。問題是這部分:'選項+ ='< option ><? echo $ result [i] ['name']; ?>< /option>';'因爲'i'變量沒有正確傳遞。我把'0'的值,然後我有整個'下拉列表'填充相同的值 – Mithrand1r 2013-03-20 10:35:00

+0

好吧,我想我發現它: '<? echo $ result(sizeof($ result);?>' - 括號的使用錯誤。 – ex3v 2013-03-20 10:41:10

+0

仍然不是這個。這個循環正在計數正確。我在我以前的評論中發佈的行是問題。 – Mithrand1r 2013-03-20 10:43:39

0

我想在你的追加行中的問題,因此要儘量redfine表變量:

var table = document.getElementById("tableToModify").getElementsByTagName("tbody")[0]; 
table.appendChild(row); 
相關問題