首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >控件的动态创建

控件的动态创建
EN

Stack Overflow用户
提问于 2011-02-03 16:55:07
回答 2查看 1K关注 0票数 0

我想创建一个用于输入销售订单详细信息的屏幕。

我想尽可能快地加快这个过程。

因此,我有一个包含两个文本框的表

代码语言:javascript
运行
复制
Product name           Qty   +

因此,如果我单击+,我想在Product name和Qty下创建新的文本框。项目的数量可以是50个或更多。我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-03 17:17:40

您可以使用jQuery。就像这样

代码语言:javascript
运行
复制
$('.plus').click(function() {
  $(this).append('<input type="text">');
})
票数 1
EN

Stack Overflow用户

发布于 2011-02-03 18:53:25

下面是一个使用初始行克隆的示例。Try it live with jsFiddle

代码语言:javascript
运行
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Add Input Rows</title>
<script type="text/javascript">
function addEvent(el, name, handler)
{
    if (typeof window.addEventListener != "undefined")
    {
        el.addEventListener(name, handler, false);
    }
    else
    {
        el.attachEvent("on" + name, handler);
    }
};

addEvent(window, "load",function()
{
    var productRows = document.getElementById("productRows");
    var clonedRow = productRows.getElementsByTagName("tr")[0].cloneNode(true);
    addEvent(document.getElementById("addProductRow"), "click", function()
    {
        productRows.appendChild(clonedRow.cloneNode(true));
    });
});
</script>
</head>
<body>

<table>
<thead>
  <tr>
    <th>Product name</th>
    <th>Qty</th>
    <th><input type="button" id="addProductRow" value="+"></th>
  </tr>
</thead>
<tbody id="productRows">
  <tr>
    <td><input type="text" name="name"></td>
    <td><input type="text" name="qty"></td>
  </tr>
</tbody>
</table>

</body>
</html>

如果您想继续添加删除动态添加的行的功能,可以这样做(jsFiddle):

代码语言:javascript
运行
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Add &amp; Remove Input Rows</title>
<script type="text/javascript">
function addEvent(el, name, handler)
{
    if (typeof window.addEventListener != "undefined")
    {
        el.addEventListener(name, handler, false);
    }
    else
    {
        el.attachEvent("on" + name, handler);
    }
}

addEvent(window, "load", function()
{
    var productRows = document.getElementById("productRows"),
        templateRow = (function() {
            var clonedRow = productRows.getElementsByTagName("tr")[0].cloneNode(true),
                removeButton = document.createElement("input");
            removeButton.type = "button";
            removeButton.value = "-";
            removeButton.title = "Click to remove this row";
            clonedRow.getElementsByTagName("td")[2].appendChild(removeButton);
            return clonedRow;
        })();

    addEvent(document.getElementById("addProductRow"), "click", function()
    {
        var row = templateRow.cloneNode(true),
            removeButton = row.getElementsByTagName("td")[2].getElementsByTagName("input")[0];
        addEvent(removeButton, "click", function()
        {
            productRows.removeChild(row);
        });
        productRows.appendChild(row);
    });
});
</script>
<style>
th.buttons input, td.buttons input { width: 100%; }
</style>
</head>
<body>

<table>
<thead>
  <tr>
    <th>Product name</th>
    <th>Qty</th>
    <th class="buttons"><input type="button" id="addProductRow" title="Click to add a new row" value="+"></th>
  </tr>
</thead>
<tbody id="productRows">
  <tr>
    <td><input type="text" name="name"></td>
    <td><input type="text" name="qty"></td>
    <td class="buttons"></td>
  </tr>
</tbody>
</table>

</body>
</html>

如果您想要在用户尝试删除已填充的行时使用恼人的弹出窗口来骚扰他们,请换成这一位(jsFiddle):

代码语言:javascript
运行
复制
addEvent(removeButton, "click", function()
{
    var inputs = row.getElementsByTagName("input"),
        confirmRemove = false;
    for (var i = 0, input; input = inputs[i]; i++)
    {
        if (input.type == "text" && input.value !== "")
        {
            confirmRemove = true;
            break;
        }
    }

    if (!confirmRemove ||
        confirm("This row contains data. Are you sure you want to remove it?"))
    {
        productRows.removeChild(row);
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4884008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档