Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Jquery删除文本输入框_Javascript_Jquery_Input - Fatal编程技术网

Javascript 使用Jquery删除文本输入框

Javascript 使用Jquery删除文本输入框,javascript,jquery,input,Javascript,Jquery,Input,请看看这个 我需要的是 当用户单击+按钮时,添加新的文本输入 当文本框计数达到3时,从第一个文本框中隐藏+按钮(用户最多可以添加2个文本框),当文本框计数小于3时取消隐藏 当用户单击x按钮时,删除父文本框(位于“从”按钮的左侧) 我的+按钮运行良好:它动态地添加文本框。但是x(移除)按钮不能。我缺少什么?您需要在单击中声明您删除事件。在您的代码中,您首先设置删除事件,然后添加它。它将不起作用,因为btn delete在添加“enter code heredelete”事件的步骤中不退出是,指示

请看看这个

我需要的是

  • 当用户单击+按钮时,添加新的文本输入
  • 当文本框计数达到3时,从第一个文本框中隐藏+按钮(用户最多可以添加2个文本框),当文本框计数小于3时取消隐藏
  • 当用户单击x按钮时,删除父文本框(位于“从”按钮的左侧)

  • 我的+按钮运行良好:它动态地添加文本框。但是x(移除)按钮不能。我缺少什么?

    您需要在
    单击
    中声明您
    删除
    事件。在您的代码中,您首先设置删除事件,然后添加它。它将不起作用,因为btn delete在添加“enter code heredelete”事件的步骤中不退出

    是,指示您当前正在设置按钮实际存在之前的删除事件。而是将删除处理程序放在add button事件中:

    但是你真的应该把你的代码放到问题中,这样其他人就可以找到它

    $(document).ready(function(){
    
        var addCvBtn    = $(".addCvBtn"),
        rmCvBtn    = $(".rmCvBtn"),
        rcmText     = $(".rcmText"),
        btncount    = 0,
        inputhtml = '<div class="cvInputContainer withRemBtn"><input placeholder="CV Linkini daxil edin" name="cvlinks[]" type="text" /><button class="btn btn-medium btn-danger rmCvBtn " type="button"><i class="icon-remove icon-white"></i></button></div>';
    
    addCvBtn.click(function(){
            if(btncount == 3)  addCvBtn.hide();
            else
            {
                $(this).parent().parent().append(inputhtml);
                btncount++;
                rmCvBtn    = $(".rmCvBtn");
                $(".withRemBtn").on('click', ".rmCvBtn", function(){
                   addCvBtn.show();
                   $(this).parent().remove(); //EDITED
                   btncount--;
                   alert('deleted');
                });
            }        
        });
    
    
    
     });
    
    $(文档).ready(函数(){
    var addCvBtn=$(“.addCvBtn”),
    rmCvBtn=$(“.rmCvBtn”),
    rcmText=$(“.rcmText”),
    BTN计数=0,
    inputtml='';
    addCvBtn.单击(函数(){
    如果(btncount==3)addCvBtn.hide();
    其他的
    {
    $(this.parent().parent().append(inputtml);
    btncount++;
    rmCvBtn=$(“.rmCvBtn”);
    $(.withRemBtn”)。在('click',.rmCvBtn',函数()上{
    addCvBtn.show();
    $(this).parent().remove();//已编辑
    BTN计数--;
    警报(“已删除”);
    });
    }        
    });
    });
    
    编辑:我认为您可能一直在尝试实现的.on()的更动态的用法是: $(文档).ready(函数(){

    var addCvBtn=$(“.addCvBtn”),
    rmCvBtn=$(“.rmCvBtn”),
    rcmText=$(“.rcmText”),
    BTN计数=0,
    inputtml='';
    addCvBtn.单击(函数(){
    如果(btncount==3)addCvBtn.hide();
    其他的
    {
    $(this.parent().parent().append(inputtml);
    btncount++;
    rmCvBtn=$(“.rmCvBtn”);
    }        
    });
    $(文档).on('click',“.withRemBtn.rmCvBtn”,函数(){
    addCvBtn.show();
    $(this).parent().remove();//已编辑
    BTN计数--;
    警报(“已删除”);
    });  
    });
    
    它删除按钮而不是文本框,所以您只需转到$(this).parent().remove();您可以根据需要调整其余部分。您可以编辑JSFIDLE吗?-但请确保完整的问题和答案也在这里。。。
        var addCvBtn    = $(".addCvBtn"),
        rmCvBtn    = $(".rmCvBtn"),
        rcmText     = $(".rcmText"),
        btncount    = 0,
        inputhtml = '<div class="cvInputContainer withRemBtn"><input placeholder="CV Linkini daxil edin" name="cvlinks[]" type="text" /><button class="btn btn-medium btn-danger rmCvBtn " type="button"><i class="icon-remove icon-white"></i></button></div>';
    
    addCvBtn.click(function(){
            if(btncount == 3)  addCvBtn.hide();
            else
            {
                $(this).parent().parent().append(inputhtml);
                btncount++;
                rmCvBtn    = $(".rmCvBtn");
            }        
        });
    
    
     $(document).on('click', ".withRemBtn .rmCvBtn", function(){
                   addCvBtn.show();
                   $(this).parent().remove(); //EDITED
                   btncount--;
                   alert('deleted');
                });  
     });