动态添加组合框

发布于 2024-08-14 14:36:32 字数 1748 浏览 3 评论 0原文

我正在尝试用 HTML 设计一个表格,当我单击“添加行”按钮时可以添加一行。如果我打算删除它,则删除添加的行也是如此。我写了一个 javascript 来添加复选框和文本。但我什至想要其中的组合框,但我被困在中间。你们能弄清楚并让我知道该怎么做吗? 这是我的 JS 文件。

function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount + 1;

        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);

        var cell4 = row.insertCell(3);
        var element3 = document.createElement("input");
        element3.type = "text";
        cell4.appendChild(element3);

        var cell5 = row.insertCell(4);
        //This is where the PROBLEM is!!
                    var element4 = document.createElement("select");
        element4.type = "option";
        cell5.appendChild(element4);

    }

    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }

        }
        }catch(e) {
            alert(e);
        }
    }

// JavaScript 文档

注意:请不要建议使用 SERVER_SIDE SCRIPTING。我只是在做 Java 脚本的作业 :)

I'm trying to design a table in HTML which can add a row when I click "add row" button & the same for deleting an added row if i intend to delete it. I have written a javascript for adding checkbox and text. But I want even combo-boxes in it and I m stuck in the middle. Could you guys just figure it out and let me know how to do that?
This is my JS file.

function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount + 1;

        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);

        var cell4 = row.insertCell(3);
        var element3 = document.createElement("input");
        element3.type = "text";
        cell4.appendChild(element3);

        var cell5 = row.insertCell(4);
        //This is where the PROBLEM is!!
                    var element4 = document.createElement("select");
        element4.type = "option";
        cell5.appendChild(element4);

    }

    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }

        }
        }catch(e) {
            alert(e);
        }
    }

// JavaScript Document

NOTE: Please dont suggest SERVER_SIDE SCRIPTING. I'm just doing my homework on Java Script :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与昨日 2024-08-21 14:36:33

这应该可以解决问题:

 var cell5 = row.insertCell(4);
 //This is where the SOLUTION is!!
 var element4 = document.createElement("select");
 var option1 = document.createElement("option");
 option1.value="1";
 option1.innerHTML="sample1";
 element4.appendChild(option1);
 var option2 = document.createElement("option");
 option2.value="2";
 option2.innerHTML="sample2";               
 element4.appendChild(option2);
  cell5.appendChild(element4);

This should do the trick:

 var cell5 = row.insertCell(4);
 //This is where the SOLUTION is!!
 var element4 = document.createElement("select");
 var option1 = document.createElement("option");
 option1.value="1";
 option1.innerHTML="sample1";
 element4.appendChild(option1);
 var option2 = document.createElement("option");
 option2.value="2";
 option2.innerHTML="sample2";               
 element4.appendChild(option2);
  cell5.appendChild(element4);
遥远的她 2024-08-21 14:36:33

试着想想你希望得到的结果。在这种情况下,您希望 HTML 看起来像这样:

    <select>
       <option></option>
       <option></option>
    </select>

那么问题是有哪些元素?我的示例中有三个,一个选择和两个选项。那么在 JavaScript 中如何创建元素呢?

 var element4 = document.createElement("select");

这将创建一个选择。那么你会如何创建一个选项呢?

   var option1 = document.createElement("option");

或许?

您如何将选项添加到选择中?与将选择添加到单元格的方式相同。

   element4.appendChild(option1);

然后创建您需要的其他选项并将选择添加到单元格中。

Try to think of the outcome that you wish to get. In this case you wish to have HTML that looks like this:

    <select>
       <option></option>
       <option></option>
    </select>

So the question is what elements are there? There are three in my example, a select and two options. So in your JavaScript how do you create elements?

 var element4 = document.createElement("select");

This creates a select. So how would you create an option?

   var option1 = document.createElement("option");

maybe?

how would you add the option to the select? Same way you add the select to the cell.

   element4.appendChild(option1);

then create the other options that you need and add the select to the cell.

や三分注定 2024-08-21 14:36:33

为什么不尝试

var sel=document.createElement("select");

// repeat this for each option you have

var opt=document.createElement("option");
opt.value="my option value";
opt.text="my option to be displayed";
sel.appendChild(opt);

// end repeat

cell5.appendChild(sel);

why not trying

var sel=document.createElement("select");

// repeat this for each option you have

var opt=document.createElement("option");
opt.value="my option value";
opt.text="my option to be displayed";
sel.appendChild(opt);

// end repeat

cell5.appendChild(sel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文