使用 JavaScript DOM 创建 HTML 表

发布于 2024-10-21 03:29:25 字数 3603 浏览 8 评论 0原文

当前的任务是根据通过 URL 传递到新页面的变量值动态创建 HTML 表。 HTML 表格需要四列:产品名称商品编号价格数量

我正在使用 JavaScript DOM 来完成此任务。但是,我无法让我的代码正常工作,因为我认为我滥用了 tBodies[0].appendChild(tr) 方法。我只能显示表格中的最后一列。不显示前 3 列。有什么想法我哪里出错了吗?

<FORM NAME=order ACTION=submit.php METHOD=GET>
<TABLE ID=gradient-style SUMMARY=EuroClassic Dynamic Ordering>
<THEAD>
<TR>
<TH scope=col>Product</th>
<TH scope=col>Item #</th>
<TH scope=col>Price</th>
<TH scope=col>Quantity</th>
</TR>
</THEAD>
<TBODY>

<SCRIPT language=javascript>

var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

var i = 0;

while(i < hashes.length)
{
    hash = hashes[i].split('=');

    vars.push(hash[0]);
    vars[hash[0]] = hash[1];

    // hasQuantity Array created to test if the Quantity Variable
    // is zero or not...is product ordered?

    var hasQuantity = new Array();
    hasQuantity = hashes[i+4].split('=');

        // hasQuantity != 0 indicates product has been ordered...

        if(hasQuantity[1] != 0)
        {
            var nameProduct = hashes[i].split('=');

            var tr = document.createElement('tr');
            var td = document.createElement('td');
            var input = document.createElement('input');

            input.type = "text";
            input.size = 40;
            input.name = nameProduct[0];

                nameProduct[1] = nameProduct[1].replace(/%/g,'');
                nameProduct[1] = nameProduct[1].replace(/2C/g,'');
                nameProduct[1] = nameProduct[1].replace(/2F/g,'');
                nameProduct[1] = nameProduct[1].replace(/28/g,'');
                nameProduct[1] = nameProduct[1].replace(/29/g,'');
                nameProduct[1] = nameProduct[1].replace(/\+/g," ");

            input.value = nameProduct[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i++;

            var nameItem = hashes[i].split('=');                

            input.type = "text";
            input.size = 6;
            input.name = nameItem[0];
            input.value = nameItem[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i = i+4;

            var namePrice = hashes[i].split('=');

            input.type = "text";
            input.size = 6;
            input.name = namePrice[0];
            input.value = namePrice[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i++;

            var nameQuantity = hashes[i].split('=');

            input.type = "text";
            input.size = 6;
            input.name = nameQuantity[0];
            input.value = nameQuantity[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);


            i++;

        }

        //If Quantity Ordered is Zero, Proceed to next set of variables

        else
        {
            i=i+7;
        }
}

</SCRIPT>


</TBODY>
</TABLE>
</FORM>

The task at hand is to dynamically create an HTML table from variable values passed to a new page through its URL. The HTML table needs four columns: Product Name, Item Number, Price, and Quantity.

I am using the JavaScript DOM to accomplish this task. However, I cannot get my code to work properly because I think I am misusing the tBodies[0].appendChild(tr) method. I can only get the last column in my table to display. The first 3 columns do not display. Any ideas where I've gone wrong?

<FORM NAME=order ACTION=submit.php METHOD=GET>
<TABLE ID=gradient-style SUMMARY=EuroClassic Dynamic Ordering>
<THEAD>
<TR>
<TH scope=col>Product</th>
<TH scope=col>Item #</th>
<TH scope=col>Price</th>
<TH scope=col>Quantity</th>
</TR>
</THEAD>
<TBODY>

<SCRIPT language=javascript>

var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

var i = 0;

while(i < hashes.length)
{
    hash = hashes[i].split('=');

    vars.push(hash[0]);
    vars[hash[0]] = hash[1];

    // hasQuantity Array created to test if the Quantity Variable
    // is zero or not...is product ordered?

    var hasQuantity = new Array();
    hasQuantity = hashes[i+4].split('=');

        // hasQuantity != 0 indicates product has been ordered...

        if(hasQuantity[1] != 0)
        {
            var nameProduct = hashes[i].split('=');

            var tr = document.createElement('tr');
            var td = document.createElement('td');
            var input = document.createElement('input');

            input.type = "text";
            input.size = 40;
            input.name = nameProduct[0];

                nameProduct[1] = nameProduct[1].replace(/%/g,'');
                nameProduct[1] = nameProduct[1].replace(/2C/g,'');
                nameProduct[1] = nameProduct[1].replace(/2F/g,'');
                nameProduct[1] = nameProduct[1].replace(/28/g,'');
                nameProduct[1] = nameProduct[1].replace(/29/g,'');
                nameProduct[1] = nameProduct[1].replace(/\+/g," ");

            input.value = nameProduct[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i++;

            var nameItem = hashes[i].split('=');                

            input.type = "text";
            input.size = 6;
            input.name = nameItem[0];
            input.value = nameItem[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i = i+4;

            var namePrice = hashes[i].split('=');

            input.type = "text";
            input.size = 6;
            input.name = namePrice[0];
            input.value = namePrice[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);

            i++;

            var nameQuantity = hashes[i].split('=');

            input.type = "text";
            input.size = 6;
            input.name = nameQuantity[0];
            input.value = nameQuantity[1];
            input.readOnly = true;

            td.appendChild(input);
            tr.appendChild(td);

            document.getElementById('gradient-style').tBodies[0].appendChild(tr);


            i++;

        }

        //If Quantity Ordered is Zero, Proceed to next set of variables

        else
        {
            i=i+7;
        }
}

</SCRIPT>


</TBODY>
</TABLE>
</FORM>

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

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

发布评论

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

评论(2

余厌 2024-10-28 03:29:25

每次有输入时,您都需要创建新的 input 和 td 元素:

td = document.createElement("td");
input = document.createElement("input");

您不需要再次“var”,并且可以继续使用现有的 tr 对象。

Every time you have an input, you need to create new input and td elements:

td = document.createElement("td");
input = document.createElement("input");

You don't need to "var" again, and you can continue using your existing tr object.

弱骨蛰伏 2024-10-28 03:29:25

我认为问题可能在于您只是不断地覆盖一个“输入”和“td”的值,但每一列都应该有一个不同的“td”和“输入”。

I think the problem may be that you're only just constantly overwriting the values of your one 'input' and 'td', but you should have a distinct 'td' and 'input' for each column.

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