如何使用jQuery复制dropdownlist中的表格行 [英] How to copy table row with dropdownlist in with jQuery

查看:86
本文介绍了如何使用jQuery复制dropdownlist中的表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,我想在按下 Add New 链接后克隆它的最后一行。它在我的行中只有TextBox的情况下工作得很好,但当下拉时没有。请帮我修改jquery代码。
下面是表格的代码:

I have a table and I want to clone the last line of it after the Add New link is pressed. It works completely fine when I have only TextBoxes in my rows but it doesn't when there's a dropdown. Please help me to modify the jquery code. Here's the code of the table:

<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>

以下是需要改进的代码:

And here's the code that needs some improvement:

 <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>

我试图修改这一行,改变 输入 转换为 选择 ,但它没有帮助

I tried to modify this line whith changing input to select, but it didnt help


var后缀= $ tr新.find(':input:first')。attr('name')。match(/ \d + /);

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


推荐答案

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

First add tbody in your table like:

        <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>

您的脚本是:

And your script is:

    <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> 

这篇关于如何使用jQuery复制dropdownlist中的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆