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

时间:2013-02-18 06:13:22

标签: jquery html textbox jquery-effects

  • 有两个内联文本框,旁边有一个“+”符号。 点击加号后,会添加一个新的文本框 用于添加和删除文本框的“+”以及“ - ”符号 分别。我使用this资源来实现我的文本框。 现在,我想只为每个文本框添加10个文本框。手段10 关键字[]的文本框和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">

1 个答案:

答案 0 :(得分:2)

好的,所以答案很简单你犯了一些逻辑错误,下面是你的代码,其中包含一些修改内容:

$(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);
    }
    });
})

});