【问题标题】:How to copy table row with dropdownlist in with jQuery如何使用 jQuery 在下拉列表中复制表格行
【发布时间】:2017-10-03 08:12:42
【问题描述】:

我有一个表,我想在按下 Add New 链接后​​克隆它的最后一行。当我的行中只有文本框时它工作得很好,但当有下拉菜单时它就不行了。请帮我修改jquery代码。 这是表格的代码:

<div><a href="#" id="addNew">Add New</a></div>
                <table id="dataTable">
                    <tr>
                        <th>Item</th>
                        <th>Cost</th>
                        <th></th>
                    </tr>
                    @if (Model != null && Model.Count > 0)
                    {
                        int j = 0;
                        foreach (var i in Model)
                        {
                            <tr>
                                <td>@Html.DropDownListFor(a => a[j].fk_purchase_id, (SelectList)ViewBag.fk_purchase_id, null, htmlAttributes: new { @class = "form-control"})</td>
                                <td>@Html.TextBoxFor(a => a[j].cost, htmlAttributes: new { @class = "form-control" })</td>
                                <td>
                                    @if (j > 0)
                                    {
                                        <a href="#" class="remove">Remove</a>
                                    }
                                </td>
                            </tr>
                            j++;
                        }
                    }
                </table>

下面是需要改进的代码:

 <script>
        $(function () {
            //1. Add new row
            $("#addNew").click(function (e) {
                e.preventDefault();
                var $tableBody = $("#dataTable");
                var $trLast = $tableBody.find("tr:last");
                var $trNew = $trLast.clone();
                alert($trNew.html);
                var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
                $trNew.find("td:last").html('<a href="#" class="remove">Remove</a>');
                $.each($trNew.find(':input'), function (i, val) {
                // Replaced Name
                var oldN = $(this).attr('name');
                var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
                 $(this).attr('name', newN);
                 //Replaced value
                var type = $(this).attr('type');
                if (type.toLowerCase() == "text") {
                 $(this).attr('value', '');
                  }
                  });

                $trLast.after($trNew);
            });

        });
    </script>

我尝试修改这一行,将 input 更改为 select,但没有帮助

var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);

【问题讨论】:

  • 我会根据herehere 的答案推荐一种不同的方法

标签: c# jquery html asp.net asp.net-mvc


【解决方案1】:

首先在您的表中添加tbody,例如:

        <table id="dataTable">
        <thead>
            <tr>
                <th>Item</th>
                <th>Cost</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            @if (Model != null && Model.Count > 0)
            {
                int j = 0;
                foreach (var i in Model)
                {
                    <tr>
                        <td>@Html.DropDownListFor(a => a[j].fk_purchase_id, (SelectList)ViewBag.fk_purchase_id, null, htmlAttributes: new { @class = "form-control"})</td>
                        <td>@Html.TextBoxFor(a => a[j].cost, htmlAttributes: new { @class = "form-control" })</td>
                        <td>
                            @if (j > 0)
                            {
                                <a href="#" class="remove">Remove</a>
                            }
                        </td>
                    </tr>
                    j++;
                }
            }
         </tbody>
        </table>

你的脚本是:

    <script>
    $(function () {
        $("#addNew").click(function (e) {
            e.preventDefault();                    
            var last = $('#dataTable>tbody>tr:last');
            if (last.length > 0) {
                var name = last.children().find('input,select')[0].name;
                var index = Number(name.replace(/[^0-9]/gi, '')) + 1;
                var tr = ('<tr>' + last.html().replace(/[0-9]+__/gi, index + '__') + '</tr>') .replace(/\[[0-9]+\]+[.]/gi, '[' + index + '].');
                $('#dataTable tbody').append(tr);
            }
        });

    });
</script> 

【讨论】:

  • 感谢@Ashiquzzaman,它有助于复制,尽管现在将数据传递给控制器​​还有另一个问题。 即使我复制了 4 行,它也最多只能传递 2 个模型项。 之前只有文本框我可以发送所有项。它仍然与 jquery 相关,可能与索引有关...
  • 你能给我看看你的html吗? 4 副本后?@Amelie
  • 这是我正在使用的完整视图:jsfiddle.net/aiste/mp5cx5sL@Ashiquzzaman
  • 对不起,你可能不明白我想要什么。请在从浏览器复制 4 后显示您的 html。您可以通过 inspect 元素获取它。
  • jsfiddle.net/aiste/6cvqpy6f @Ashiquzzaman,现在是复制了 4 项的表格的 html
猜你喜欢
  • 2016-06-16
  • 2011-05-24
  • 1970-01-01
  • 2011-10-17
  • 2011-10-14
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2016-12-27
相关资源
最近更新 更多