0

我正在尝试将字段集克隆为名称 + 1、2、3 等等以及输入 ID,但是在克隆一对工人补偿字段集后,当我单击另一个字段集类型时,它会在前一个克隆后给出其输入 ID像 4 5 6 这样的字段集作为 id。我是一名新手程序员,所以请帮助我。附加图片以了解更多信息并在有问题时回复我。

<script type="text/javascript">

function cloneit(a,b)
{

        var fieldset = a;
        var fi_class = b;
        //alert(fi_class);

        var count = fi_class.split(/woc_class1/g).length - 1;
        alert(count);

        if(count == 1)
        {
        var newNum = new Number(1);
        }
        else
        {
        var num = $('.cloned').length;
        var newNum = new Number(num + 1);
        }

        var newElem = $(fieldset).clone().attr('id', fieldset + newNum).addClass('cloned');

        $(newElem).css('margin-top','20px');
        //set all div id's and the input id's
        newElem.children('div').each(function(i) {
            this.id = 'input' + (newNum * 5 + i);
        });

        newElem.find('input').each(function() {
            this.id = this.id + newNum;
            this.name = this.name + newNum;

        });

        if (num > 0) {
            $('.cloned:last').after(newElem);
        } else {
            $(fieldset).after(newElem);
        }

        $('#delete').removeAttr('disabled');

        if (newNum == 5) $('#add').attr('disabled', 'disabled');

 }   

function remclone()
    {
        $('.cloned:last').remove();
        $('#add').removeAttr('disabled');
        if ($('.cloned').length == 0) {
            $('#delete').attr('disabled', 'disabled');
        }
    }
</script>

截屏

这是它的html

<fieldset id="woc_fieldset">
    <legend>Worker Compensation</legend>
    <table border="0">
    <tr>
    <td>Carier </td>
    <td><input type="text" name="worker_compensation_carrier" id="worker_compensation_carrier" value="<?php echo $a->worker_compensation_carrier; ?>" required="required"/></td>
    </tr>
    <tr>
    <td>Phone Number</td>
    <td><input type="text" name="worker_compensation_phone" id="worker_compensation_phone" class="phone" value="<?php echo $a->worker_compensation_phone; ?>" required="required"/></td>
    </tr>
    <tr>
    <td>Premium</td>
    <td><input type="text" name="worker_compensation_premium" id="worker_compensation_premium" value="<?php echo $a->worker_compensation_premium; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Renewal Date</td> 
    <td><input type="text" name="worker_compensation_renewal_date" id="worker_compensation_renewal_date" value="<?php echo $a->worker_compensation_renewal_date; ?>"  required="required"/></td>
    </tr>
    </table>
    <button type="button" id="delete" onclick="remclone();">Delete</button>
</fieldset>
<button type="button" id="add" onclick="cloneit('#woc_fieldset');">Add</button>



<fieldset id="two">
    <legend>General Liability</legend>
    <table border="0">
    <tr>
    <td>Carier</td>
    <td><input type="text" name="general_liability_carrier" id="general_liability_carrier" value="<?php echo $b->general_liability_carrier; ?>" required="required"/></td>
    </tr>
    <tr>
    <td>Phone Number</td>
    <td><input type="text" name="general_liability_phone" id="general_liability_phone" value="<?php echo $b->general_liability_phone; ?>" required="required"/></td>
    </tr>
    <tr>
    <td>Premium</td>
    <td><input type="text" name="general_liability_premium" id="general_liability_premium" value="<?php echo $b->general_liability_premium; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Renewal Date</td>
    <td><input type="text" name="general_liability_renewal_date" id="general_liability_renewal_date" value="<?php echo $b->general_liability_renewal_date; ?>" required="required" /></td>
    </tr>
    </table>

</fieldset>


<fieldset id="three">
    <legend>Group Health</legend>
    <table border="0">
    <tr>
    <td>Carier</td>
    <td><input type="text" name="group_health_carrier" id="group_health_carrier" value="<?php echo $c->group_health_carrier; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Phone Number</td>
    <td><input type="text" name="group_health_phone" id="group_health_phone" value="<?php echo $c->group_health_phone; ?>"  required="required" /></td>
    </tr>
    <tr>
    <td>Premium</td>
    <td><input type="text" name="group_health_premium" id="group_health_premium" value="<?php echo $c->group_health_premium; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Renewal Date</td>
    <td><input type="text" name="group_health_renewal_date" id="group_health_renewal_date" value="<?php echo $c->group_health_renewal_date; ?>"  required="required"/></td>
    </tr>
    </table>

</fieldset>



<fieldset id="four">
    <legend>Property</legend>
    <table border="0">
    <tr>
    <td>Carier</td>
    <td><input type="text" name="property_carrier" id="property_carrier" value="<?php echo $d->property_carrier; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Phone Number</td>
    <td><input type="text" name="property_phone" id="property_phone" value="<?php echo $d->property_phone; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Premium</td>
    <td><input type="text" name="property_premium" id="property_premium" value="<?php echo $d->property_premium; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Renewal Date</td>
    <td><input type="text" name="property_renewal_date" id="property_renewal_date" value="<?php echo $d->property_renewal_date; ?>"  required="required"/></td>
    </tr>
    </table>
</fieldset>


<fieldset id="five">
    <legend>Commercial Auto</legend>
    <table border="0">
    <tr>
    <td>Carier</td>
    <td><input type="text" name="commercial_auto_carrier" id="commercial_auto_carrier" value="<?php echo $e->commercial_auto_carrier; ?>"  required="required"/></td>
    </tr>
    <tr>
    <td>Phone Number</td>
    <td><input type="text" name="commercial_auto_phone" id="commercial_auto_phone" value="<?php echo $e->commercial_auto_phone; ?>" required="required" /></td>
    </tr>
    <tr>
    <td>Premium</td>
    <td><input type="text" name="commercial_auto_premium" id="commercial_auto_premium" value="<?php echo $e->commercial_auto_premium; ?>" required="required" /></td>
    </tr>
    <tr>
    <td>Renewal Date</td>
    <td><input type="text" name="commercial_auto_renewal_date" id="commercial_auto_renewal_date" value="<?php echo $e->commercial_auto_renewal_date; ?>" required="required" /></td>
    </tr>
    </table>

</fieldset>
4

1 回答 1

0
function cloneit(a,b)
{

        var fieldset = a;
        var fi_class = b;
        alert(fi_class);


        var num = $('.'+fi_class).length;
        alert(num);
        var newNum = new Number((num - 1) +1);


        var newElem = $(fieldset).clone().attr('id', fieldset + newNum);

        $(newElem).css('margin-top','20px');
        //set all div id's and the input id's
        newElem.children('div').each(function(i) {
            this.id = 'input' + (newNum * 5 + i);
        });

        newElem.find('input').each(function() {
            this.id = this.id + newNum;
            this.name = this.name + newNum;

        });

        if (num > 0) {
            $('.'+fi_class+':last').after(newElem);
        } else {
            $(fieldset).after(newElem);
        }

        $('#delete').removeAttr('disabled');

        if (newNum == 5) $('#add').attr('disabled', 'disabled');

 }   

function remclone()
    {
        $('.'+fi_class+':last').remove();
        $('#add').removeAttr('disabled');
        if ($('.cloned').length == 0) {
            $('#delete').attr('disabled', 'disabled');
        }
    }
于 2013-09-06T06:21:25.163 回答