2
  • 有两个内联文本框,旁边有一个“+”号。单击加号时,会添加一个新文本框,它将具有“+”和“-”符号,分别用于添加和删除文本框。我使用这个资源来实现我的文本框。现在,我只想为每个文本框添加 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">
4

1 回答 1

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

});

于 2013-02-18T09:01:33.940 回答