0

我正在尝试限制以下链接上的克隆数量:

JSFiddle 链接

这是 Jquery 编码:

$("#addbt").click(function () {
 $('#choice').clone()
     .attr('id', 'choice' + $('.ddl').length)
     .attr('name', 'choice' + $('.ddl').length)
     .insertAfter(".ddl:last");
 $('#num').clone()
     .attr('id', 'num' + $('.dd2').length)
     .attr('name', 'num' + $('.dd2').length)
     .insertAfter(".dd2:last");});

 $("#removebt").click(function () {
   $("#choice1").remove();
   $("#num1").remove();
 });

我把它放在 JSFiddle 上,这样每个人都可以看到它当前的功能编码。

4

1 回答 1

1
$("#addbt").click(function () {

   // check count
   var clonedCount = $('[id^=choice]').length; // OR $('.ddl').length @Matt mentioned

   if( clonedCount >  3) return false;   // stop clonning

   $('#choice').clone()
        .attr('id', 'choice' + $('.ddl').length)
        .attr('name', 'choice' + $('.ddl').length)
        .insertAfter(".ddl:last");
    $('#num').clone()
        .attr('id', 'num' + $('.dd2').length)
        .attr('name', 'num' + $('.dd2').length)
        .insertAfter(".dd2:last");
});

JSFiddle 链接

于 2013-03-09T20:50:25.257 回答