Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

    <div class="rows">
       <input type="text" id="ip1" class="ip1">
       <input type="text" id="ip2" class="ip2">
       <input type="button" value="Delete" id="del" class="del">                        
    </div><!--Rows-->

    <script>
      $(document).ready(function(e){
         $("#add").click(function(e){
            $(".rows").append('<input type="text" id="ip1" class="ip1">'+
            '<input type="text" id="ip1" class="ip1">'+
            '<input type="button" alue="Delete" id="del" class="del">');
         });
      });
    </script>

I Did this using this method. Is there any other good method like appending the whole div. and i need to delete that row alone if i click delete button, Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

To avoid duplicated id's you can do that in this way : http://jsbin.com/xotoyogo/1/edit?html,js,output

JS:

  $(document).ready(function(e){
         $("#add").click(function(e){
           var newId = $('.row').length + 1;

           $(".container")
           .append('<div class="row"><input type="text" id="ip' + newId+'" class="ip1"> '+
          '<input type="text" id="ip'+newId+'" class="ip1"> '+
          '<input type="button" value="Delete" id="del'+newId+'" class="del"></div>');
         });
      });

HTML:

<div class="container">
<div class="row">
       <input type="text" id="ip1" class="ip1">
       <input type="text" id="ip2" class="ip2">
       <input type="button" value="Delete" id="del" class="del"> 

    </div><!--Rows-->
  </div>
  <button id="add">add</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...