Jquery如何使用动态添加的字段为name属性添加计数器

时间:2015-11-28 20:24:52

标签: javascript jquery forms validation

您好我有一个包含这两个字段的表单,用户可以动态添加其他字段。

 <section class="multi-field-wrapper">                          
 <h2>Ingredients</h2>
 <div class="multi-fields">
 <div class="f-row ingredient">
 <input type="text" name="ingredients[]"placeholder="Ingredients *"/>
 <input type="text"  name="quantity[]" placeholder="Quantity" />
 <button type="button" class="remove">-</button>
 </div>
 </div>
 <div class="f-row full">
 <button type="button" class="add">Add an ingredient</button>
 </div>
 </section>

<script>
$('.multi-field-wrapper').each(function() {
    var $wrapper = $('.multi-fields', this);
    $(".add", $(this)).click(function(e) {
        $('.f-row.ingredient:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
   });
    $('.f-row.ingredient .remove', $wrapper).click(function() {

        if ($('.f-row.ingredient', $wrapper).length > 1)
            $(this).parent('.f-row.ingredient').remove();
    });
});
</script>


<script>
$('.multi-field-wrapper').each(function() {
    var $wrapper = $('.multi-fields', this);
    $(".add", $(this)).click(function(e) {
        $('.f-row.instruction:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('')
    });
    $('.f-row.instruction .remove', $wrapper).click(function() {

        if ($('.f-row.instruction', $wrapper).length > 1)
            $(this).parent('.f-row.instruction').remove();
    });
});
</script>

我正在使用** Jquery Validation插件,之前我已阅读过所有主题,并且我知道我不能将SAME NAME用于超过1个输入字段。我需要的是一种方法将NAME属性的计数器或索引添加到上面的JQUERY SCRIPT和 NOT 另一个附加结果的脚本,以便得到这样的结果:

领域1:成分[0] 领域2:成分[1] .......等等

如何在验证后删除括号之间的索引?

谢谢!

0 个答案:

没有答案