jquery使用bind添加和删除输入

jquery使用bind添加和删除输入,jquery,Jquery,我只想添加两行。下面的代码有两个问题 不停地添加两行以上是很困难的 当我尝试删除时,它不会删除所选记录 这是演示 $('#addProfile').bind('click', function() { if($('#container').find('#removeProfile').length < 2) { var len = $('#container').find('#removeProfile').length;

我只想添加两行。下面的代码有两个问题

  • 不停地添加两行以上是很困难的
  • 当我尝试删除时,它不会删除所选记录
  • 这是演示

    $('#addProfile').bind('click', function() {
    
                if($('#container').find('#removeProfile').length < 2) {
    
                    var len = $('#container').find('#removeProfile').length;
                    //alert(len);
                    var index = len+1;
    
                    $('#container').append('<div>&nbsp;</div><div class ="form-group-inline"><label class="col-sm-2 control-label" >Profile Number</label><div class="col-sm-3"><input  class="text-input form-control" type="text" name="profileNumber" id="profileNumber" /></div><label class="col-sm-2 control-label" >Amount</label><div class="col-sm-3"><input  class="text-input form-control" type="text" name="amount" id="amount" /></div><div class="col-sm-2"><button type="button" id="removeProfile" class="btn btn-success">RemoveProfile</button></div></div>');
    
    
                }else{
    
                    bootbox.alert("You cannot add more than two profiles!", function() {});
    
                }
            })
     });
    
      $('body').on('click', '#removeProfile', function() {
    if($('#container').find('#removeProfile').length > 0) {            
        $(this).parent().remove();
    
     }
    })
    
    $('addProfile').bind('click',function(){
    if($(“#容器”).find(“#removeProfile”).length<2{
    var len=$('#container').find('#removeProfile').length;
    //警报(len);
    var指数=len+1;
    $(“#容器”).append('Profile numbermountremoveprofile');
    }否则{
    alert(“您不能添加两个以上的配置文件!”,function(){});
    }
    })
    });
    $('body')。在('click','removeProfile',function()上{
    if($('#container').find('#removeProfile').length>0{
    $(this.parent().remove();
    }
    })
    
    我可以在我的工作区中添加记录,但相同的代码在fiddle中不起作用。我希望有人能帮助我,首先让它在小提琴工作,然后看看我的问题。谢谢

    当您执行
    $(“#容器”).find(“#removeProfile”).length时,您将始终得到0或1,而不是更多。
    无论如何,同一个ID不能有两次。删除ID或使其唯一。().

    相反,您可以使用
    .form group inline
    来检查您有多少。 我们也要这样做:


    这应该行得通。

    不确定您的HTML是什么样子,但看到您的代码让我觉得它是无效的,因为您不能有多个ID为removeProfile的元素。这也是你的问题
    .length
    在尝试获取具有相同ID的元素的长度时将始终返回1。我只有一个带有“removeProfile”的ID。whic在上面的container append code中。不,当您单击时,您正在追加一个新的元素。当我动态添加这样的元素时,我还可以使用什么来查找legnth?每次您将HTML追加到container div中时,您都会创建一个id为
    removeProfile
    的新元素。我建议您将其切换到一个类,并使用
    on()
    确定单击了哪个配置文件容器。我将尝试此操作,同时我已使用id而不是名称更新了我的原始帖子。移除零件有用吗?我试过了。添加部分正在工作。它能够计数和报警。但“删除部分”仍不起作用。它与代码的作用相同,只是使用了不同的选择器。可能您需要删除父项的父项。。我会更新答案。你有更新答案的方法吗?谢谢你!
    if($('#container .form-group-inline').length < 2) { .... }
    
    $('body').on('click', '.form-group-inline .btn-success', function() { 
        if($('#container .form-group-inline').length > 0) {            
            $(this).parents('.form-group-inline').remove();
        }
    }