0

嗨,我试图让连锁选择克隆但没有运气。有人可以帮我看看如何做到这一点吗?我可以克隆常规输入字段,但不能克隆动态命名字段,例如选择。

我编辑了删除链式脚本的脚本。我希望这有帮助。

如果这仍然是太多的代码。有人可以建议克隆链式选择以增加 id 和 name 吗?我对 Jquery 真的很陌生,任何帮助都会很棒。

下面是我正在使用的代码..

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#add').click(function() { // how many "duplicatable" input fields we currently 
            have
            var num = $('.print-input-style').length; // the numeric ID of the new input field 
            being added
            var newNum = new Number(num + 1);
            var currentContainerId = '#print-input-' + num;

            // create the new element via clone(), and manipulate it's ID using newNum
            var newElem = $(currentContainerId).clone().attr('id', 'print-input-' + newNum);

            // manipulate the name/id values of the input inside the new element
            $(currentContainerId).children('label').each(function(i) {
                var attrFor = $(this).attr('for');
                $(this).attr('for', attrFor.substring(0, attrFor.lastIndexOf("-") + 1) + newNum);
            });
            $(currentContainerId).children('select').each(function(i) {
                var attrId = $(this).attr('id');
                var attrName = $(this).attr('name');
                $(this).attr('id', attrId.substring(0, attrId.lastIndexOf("-") + 1) + newNum)
                    .attr('name', attrName.substring(0, attrName.lastIndexOf("-") + 1) + newNum);
            });
            $(currentContainerId).children('input').each(function(i) {
                var attrId = $(this).attr('id');
                var attrName = $(this).attr('name');
                $(this).attr('id', attrId.substring(0, attrId.lastIndexOf("-") + 1) + newNum)
                    .attr('name', attrName.substring(0, attrName.lastIndexOf("-") + 1) + newNum);
            });

            // insert the new element after the last "duplicatable" input field
            $(currentContainerId).after(newElem);

            // business rule: you can only add 4 names    
            if (newNum == 50) $('#add-customer').attr('disabled', 'disabled');
        });
    });
</script>
<div id="print">
    <br/>
    <form action="">
        <div id="print-input-1" class="print-input-style">
            <select id="print-mark-1" name="print-mark-1">
                <option value="">--</option>
                <option value="bmw">BMW</option>
                <option value="audi">Audi</option>
            </select>
            <select id="print-series-1" name="print-series-1">
                <option value="">--</option>
                <option value="series-3" class="bmw">3 series</option>
                <option value="series-5" class="bmw">5 series</option>
                <option value="series-6" class="bmw">6 series</option>
                <option value="a3" class="audi">A3</option>
                <option value="a4" class="audi">A4</option>
                <option value="a5" class="audi">A5</option>
            </select>
            <script>
                $("#print-mark-1").chained("#print-series-1");
                /* or $("#series").chainedTo("#mark");  
                 */
            </script>
            <br/>
        </div>
        <input type=submit value=submit>
    </form>
    <br/>
    <br/>
    <div id="div-add">
        <input id="add" value=add width="20" height="20" type="Button" />
    </div>
</div>
4

0 回答 0