繁体   English   中英

jQuery选择不适用于克隆行

[英]jQuery chosen not working for cloned row

这是我的HTML:

<table>
        <tr>
            <th>Names</th>
            <th>Product Names</th>
        </tr>
        <tr class="data-wrapper">
            <td>
                <select class="form-control required chosen-select-width name" name="source_language[][0]" aria-required="true">
                    <option value="name1">Name 1</option>
                    <option value="name2">Name 2</option>
                    <option value="name3">Name 3</option>
                    <option value="name4">Name 4</option>
                    <option value="name5">Name 5</option>
                </select>
            </td>
            <td><input type="text" name="product-names"></td>
        </tr>
    </table>
    <button type="button" class="btn add-new-data"> Add </button>

在这里,我使用了jQuery选择的插件来下拉。 我正在克隆这一行。 但是,克隆后所选的选择不起作用。 这是我在jsFiddle上的代码

如何为克隆元素选择工作?

你应该先克隆和应用chosen以后这样的克隆元素“选择自由”。

jQuery(function($){ 
    var clone = $("table tr.data-wrapper:first").clone(true);
    $('select.name').chosen({width: "100%"});
    $('body').on('click', '.add-new-data', function() {
        var ParentRow = $("table tr.data-wrapper").last();
        clone.clone(true).insertAfter(ParentRow);
        $('tr.data-wrapper:last select').chosen();
    });
});

DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM