1

I have a function:

<?php $max = 5; ?>

<script type="text/javascript">
jQuery(function($) {
    var scntDiv = $('#div');
    var i = $('#div p').size() + 2;
    var x = <?php echo $max;?>;
    var a = 0;
    $('#add').click (function() {
        $('<p><input type="text" id="" name="field['+a+'][0]" value="" placeholder="" /><a href="#" id="delete">Remove</a></p>').appendTo(scntDiv);
        i++;
        a++;
        return false;
    });
    $('#delete').live('click',function() {
        if( i > 2 ) {
        $(this).parents('p').remove();
        i--;
        }
        return false;
    });
});
</script>

As you can see var x is constant and var a iterates in every click of add button. How to make these two variables sum each other with every iteration and to replace +a+ in name="field['+a+'][0]"?

4

1 回答 1

3

因此,要从评论中提取它,在这种情况下最简单的解决方案是:

改变:

var a = 0;

经过

var a = x;
于 2014-03-21T18:24:13.997 回答