2013-02-18 53 views
2
  • 有两个内联的文本框,在它们旁边有一个“+”号。 单击加号后,会添加一个新的文本框,它将分别带有“+”和“ - ”号以便分别添加和删除文本框 。我使用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"> 
+0

为您需要的弹跳效果http://gsgd.co.uk/sandbox/jquery/easing/ – 2013-02-18 06:15:21

+0

@JonathandeM。我使用了'jquery-ui.min.js'。它提供了反弹效果。但在我的代码中没有渲染效果。我已经将问题中的资源链接到了他们展示的演示中。 – SilentAssassin 2013-02-18 06:20:25

+0

你可以给我们http://jsfiddle.net/链接包括CSS,图像pathes到你的serwer,或者至少:'.right img {background:red;宽度:30px!重要; height:30px!important;}' – bumerang 2013-02-18 08:03:48

回答

2

好了,答案很简单,你已经做了一些逻辑错误,下面是你的代码,其中描述了一些修补程序n评论:

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

});

+1

我也删除了代码'style =“display:none”'查看结果。 – bumerang 2013-02-18 09:08:57

+0

在关键字的文本框中,长度应该是10.保留它20添加20条记录! 另外,我在删除文本框时遇到了一些问题。但是,如你所提到的改变选择器解决了这个问题。 并检查我提到的资源的演示。点击“+”号时发生的反弹效应在我的情况下不会发生。文本框之间的间距也不像它在演示中的样子。这是我的文本框[出现](http://my.jetscreenshot.com/18510/20130218-bbhp-25kb) – SilentAssassin 2013-02-18 09:45:52

+1

@SilentAssassin - __在关键字的文本框的长度应该是10.保持它20添加20记录 !__ - 是的,但是它是你的代码,对于那些差距,它是你添加到这些新元素的类的依据,你给他们'txt_div',类但你应该给'text_div',就像第一个有 – bumerang 2013-02-18 10:39:47