无法克隆链式选择

时间:2013-11-19 16:39:57

标签: jquery select chained

您好我正在尝试将链接选择克隆而没有运气。有人可以帮我解决这个问题吗?我可以获取常规输入字段进行克隆,但不能获得动态命名字段,例如select。

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

如果这仍然是太多的代码。有人可以建议一个isy来克隆链式选择,增加id和名称吗?我对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>

0 个答案:

没有答案