jquery - 动态添加多个文本框的问题

标签 jquery html textbox jquery-effects

  • 有两个内联文本框,它们旁边有一个“+”号。 单击加号会添加一个新的文本框,它将具有 “+”和“-”符号用于添加和删除文本框 分别。我用了this资源来实现我的文本框。 现在,我只想为每个文本框添加 10 个文本框。表示 10 keyword[] 的文本框和 link_name[] 的 10 个文本框 请参阅输入标签的名称。但是使用这段代码它不起作用。

    如果我继续为关键字[]添加文本框,那么 19 个文本框是 添加,然后如果我尝试为 link_name[] 添加一个文本框,那么它 不添加单个文本框并显示已达到的最大限制。

    如果反之亦然,则可以正常工作。

  • 还有一个问题是反弹效果不起作用。不多 熟悉效果所以找不到原因 工作。

jQuery 和 HTML 如下所示:

jQuery

$(document).ready(function() {
    var id_1 = 2, max = 9, append_data;
    /*TEXT BOXES FOR LINK NAMES*/   
    /*If add_1 icon was clicked*/
    $("#add_1").live('click', function(){
        if($("div[id^='txt_']").length <9){ //Don't add new text box if limit is reached
            $(this).remove(); //remove add icon from the current text box
            var append_data = '<div id="txt_'+id_1+'" class="txt_div" style="display:none;"><div class="left"><input type="text" id="input_'+id_2+'" name="link_name[]"/></div><div class="right"><img src="add.png" id="add_1"/> <img src="remove.png" id="remove_1"/></div></div>';
            $("#textboxes_1").append(append_data); //append new text box in main div
            $("#txt_"+id_1).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
            id_1++;
        }
        else{
            alert("Maximum 10 textboxes are allowed");  
        }
    })
    $("#remove_1").live('click',function(){
        var prev_obj = $(this).parents().eq(1).prev().attr('id'); //prev div id of this text_box
        $(this).parents().eq(1).slideUp('medium', function() { $(this).remove(); //remove this text box with a slide up
        if($("div[id^='txt_']").length >1){
            append_data = '<img src = "remove.png" id = "remove_1"/>';
        }
        else{
            append_data = '';
        }
        if($("#add_1").length< 1){
            $("#"+prev_obj+" .right").html('<img src = "add.png" id = "add_1"/>'+append_data);
        }
        });
    })

/*TEXT BOXES FOR KEYWORDS*/

    /*If add_2 icon was clicked*/
    var id_2 = 12, max = 19;
    $("#add_2").live('click', function(){
        if($("div[id^='txt_']").length <19){ //Don't add new text box if limit is reached
            $(this).remove(); //remove add icon from the current text box
            var append_data = '<div id="txt_'+id_2+'" class="txt_div" style="display:none;"><div class="left"><input type="text" id="input_'+id_2+'" name="keyword[]"/></div><div class="right"><img src="add.png" id="add_2"/> <img src="remove.png" id="remove_2"/></div></div>';
            $("#textboxes_2").append(append_data); //append new text box in main div
            $("#txt_"+id_2).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
            id_2++;
        }
        else{
            alert("Maximum 10 textboxes are allowed");  
        }
    })
    $("#remove_2").live('click',function(){
        var prev_obj = $(this).parents().eq(1).prev().attr('id'); //prev div id of this text_box
        $(this).parents().eq(1).slideUp('medium', function() { $(this).remove(); //remove this text box with a slide up
        if($("div[id^='txt_']").length >1){
            append_data = '<img src = "remove.png" id = "remove_2"/>';
        }
        else{
            append_data = '';
        }
        if($("#add_2").length< 1){
            $("#"+prev_obj+" .right").html('<img src = "add.png" id = "add_2"/>'+append_data);
        }
        });
    })
});

HTML

<div id="textboxes_1" class="inline">
    <div id="text_1" class="text_div">
        <div class="left">
            <input type="text" id="input_1" value="Enter URL(s) here" name="link_name[]" />
        </div>
        <div class="right">
            <img src="add.png" id="add_1" />
        </div>
    </div>
</div>
<div id="textboxes_2" class="inline">
    <div id="text_11" class="text_div">
        <div class="left">
            <input type="text" id="input_11" value="Enter Keyword(s) here" name="keyword[]" />
        </div>
        <div class="right">
            <img src="add.png" id="add_2" />
        </div>
    </div>
</div>
<div style="clear:left;"></div>
<input type="submit" id="submit" name="submit" value="SUBMIT">

最佳答案

好的,所以答案很简单你犯了一些逻辑错误,下面是你的代码以及评论中描述的一些修复:

$(document).ready(function() {
var id_1 = 2, max = 9, append_data;
/*TEXT BOXES FOR LINK NAMES*/   
/*If add_1 icon was clicked*/
$("#add_1").live('click', function(){
    if($("#textboxes_1 input").length <10){ //Don't add new text box if limit is reached
// Here You have to check #textboxes_1 for his own input's, and You have to give 10 not 9, becouse lenght is allways actual number of elements

        $(this).remove(); //remove add icon from the current text box
        var append_data = '<div id="txt_'+id_1+'" class="txt_div"><div class="left"><input type="text" id="input_'+id_1+'" name="link_name[]"/></div><div class="right"><img src="add.png" id="add_1"/> <img src="remove.png" id="remove_1"/></div></div>';
// in the code abowe You give id="input_'+id_2+'", I belive it should be id="input_'+id_1+'"
        $("#textboxes_1").append(append_data); //append new text box in main div
        $("#txt_"+id_1).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
        id_1++;
    }
    else{
        alert("Maximum 10 textboxes are allowed");  
    }
})
$("#remove_1").live('click',function(){
    var prev_obj = $(this).parents().eq(1).prev().attr('id'); //prev div id of this text_box
    $(this).parents().eq(1).slideUp('medium', function() { $(this).remove(); //remove this text box with a slide up
    if($("div[id^='txt_']").length >1){
        append_data = '<img src = "remove.png" id = "remove_1"/>';
    }
    else{
        append_data = '';
    }
    if($("#add_1").length< 1){
        $("#"+prev_obj+" .right").html('<img src = "add.png" id = "add_1"/>'+append_data);
    }
    });
})

/*TEXT BOXES FOR KEYWORDS*/

/*If add_2 icon was clicked*/
var id_2 = 12, max = 19;
$("#add_2").live('click', function(){
    if($("#textboxes_2 input").length <20){ //Don't add new text box if limit is reached
// The same issue was here as well

        $(this).remove(); //remove add icon from the current text box
        var append_data = '<div id="txt_'+id_2+'" class="txt_div" ><div class="left"><input type="text" id="input_'+id_2+'" name="keyword[]"/></div><div class="right"><img src="add.png" id="add_2"/> <img src="remove.png" id="remove_2"/></div></div>';
        $("#textboxes_2").append(append_data); //append new text box in main div
        $("#txt_"+id_2).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
        id_2++;
    }
    else{
        alert("Maximum 10 textboxes are allowed");  
    }
})
$("#remove_2").live('click',function(){
    var prev_obj = $(this).parents().eq(1).prev().attr('id'); //prev div id of this text_box
    $(this).parents().eq(1).slideUp('medium', function() { $(this).remove(); //remove this text box with a slide up
    if($("div[id^='txt_']").length >1){
        append_data = '<img src = "remove.png" id = "remove_2"/>';
    }
    else{
        append_data = '';
    }
    if($("#add_2").length< 1){
        $("#"+prev_obj+" .right").html('<img src = "add.png" id = "add_2"/>'+append_data);
    }
    });
})

});

关于jquery - 动态添加多个文本框的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14930481/

相关文章:

jquery - axios post数据格式

javascript - 在 jquery 中跳过 $.each 的最后一个索引

html - 简单表单添加填充并移动表单上的一些元素

javascript - setTimeout 不添加延迟

javascript - JS 对象值以 'undefined' 开头

JavaScript -> CSS(显示 :none to display:block)

php - 通过POST方法将复选框数据插入mysql数据库

Javascript 只允许文本值

c# - WPF:修剪所有文本框

python - 如何增加文本小部件的字体大小?