Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 克隆div并增量重命名元素ID_Php_Javascript_Jquery_Forms_Clone - Fatal编程技术网

Php 克隆div并增量重命名元素ID

Php 克隆div并增量重命名元素ID,php,javascript,jquery,forms,clone,Php,Javascript,Jquery,Forms,Clone,好吧,假设我有一个div,里面有form元素。我希望能够使用jQuery通过单击按钮克隆该div,并添加表单的版本2,这样所有元素ID的名称都将增加1 <div id="card"> <!-- PART 1 --> <h1 class="card_build">Build your card options:</h1> <select id="country" name="country[]">

好吧,假设我有一个div,里面有form元素。我希望能够使用jQuery通过单击按钮克隆该div,并添加表单的版本2,这样所有元素ID的名称都将增加1

<div id="card">
    <!-- PART 1 --> 
    <h1 class="card_build">Build your card options:</h1>

    <select id="country" name="country[]">
        <?php
            include('lib/class_dbcon.php');
            $connect = new doConnect();

            $q = mysql_query("SELECT * FROM country ORDER BY country_id ASC");
            while($row = mysql_fetch_assoc($q))
            {
                echo '<option value="'.$row['country_id'].'">'.$row['country_option'].'</option>';
            }
        ?>
    </select>

    <select id="filter" name="filter[]">
        <option value="">-- Select Filter --</option>
    </select>

    <select id="load_choice" name="load_choice[]">
        <option value="">-- Select Load_choice --</option>
    </select>

    <select id="plastic" name="plastic[]">
        <option value="">-- Select Plastic --</option>
    </select>

    <select id="UG_tipping" name="UG_tipping[]">
        <option value="">-- Select UG/Tipping --</option>
    </select>
    <!-- PART 1 -->
    <!-- PART 2 -->
    <div id="part2" style="margin-top:10px;">
    <h1 class="card_build">Customize the card:</h1>
    <input type="text" name="3rdLine" size="32" class="field" id="3rdLine">
    <input type="text" name="4thLine" size="32" class="field" id="4thLine">
    <input type="text" name="card_value" size="32" class="field" id="card_value">
    <label for="showpoints">Show "Points"?</label>
    <input type="checkbox" value="points" class="checkbox" checked="checked">
    <label for="cobrand">Co-branded?</label>
    <input type="checkbox" value="cobrand" class="checkbox" checked="checked">
    <textarea rows="5" name="message" class="textarea" id="message"></textarea>
    <hr>
    </div>
    <!-- PART 2 -->
</div>
    <a href="#" onCLick="moreFields()">ADD</a>
因此,如果您查看这段代码,并在最后单击ADD链接,它将复制并将其转换为,并对div中的所有元素id执行相同的操作。工作中的一个难题是,我希望最多有5个克隆,因此脚本只能增加4倍或5倍。这其实并不重要,只要我能找到创建最大克隆的方法


我唯一的另一个问题是,当一个div被克隆时,PHP注入是否仍然有效?提前谢谢,我整晚都在绞尽脑汁想这个问题。

从你的代码中,我认为你的主要问题是访问下拉列表组时没有冲突。如果是这样的话,我认为这是可以实现的,而不必为创建的每个组增加每个元素的id。如果我要这样做,我将按如下方式处理

首先,DOM是一个示例:


<div id="card">
    <div class="group">
        <select id="country" name="country[]">
            <option>select</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
        <select id="filter" name="filter[]">
            <option>select</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
    </div>
</div>
<a id="more" href="">More</a>
然后jquery:


$(function(){
    var newgroup = $('<div>').addClass('group');
    $('#more').click(function(e){
        e.preventDefault();
        $('.group').first().clone().appendTo(newgroup).appendTo('#card');
    });

    $('.group #country').live('change',function(){
        $(this).parent().find('#filter').val(1);
    });
});

这将创建一组新的表单元素,并附加和它们相关联的事件。