0

ボタンをクリックした後に最後に行を追加する動的テーブルを作成しました。1 つの列に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、2つのオプションがあります。<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>

しかし、どういうわけか私のコードは機能しません...誰かが私が間違っていることのヒントを教えてくれますか?

4

2 に答える 2

0

行の追加に問題があると思うので、テーブル変数を再調整してみてください:

var table = document.getElementById("tableToModify").getElementsByTagName("tbody")[0];
table.appendChild(row);
于 2013-03-20T11:03:29.570 に答える
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()関数はその入力パラメーターをコピーするため、その後は、試みた方法でアクセスできなくなります。

于 2013-03-20T10:30:06.997 に答える