简体   繁体   中英

Add Remove Button on Cloned Field

Good day great people, I need help to add Remove button after the cloned happen. The Remove button should remove the cloned field. Any suggestion would be appreciated. Bootply Version - http://www.bootply.com/LiqkgWUFF6

Thanks in advance.

 var template = $('#line_1').clone(); $('#cloneButton').click(function () { var rowId = $('.row').length + 1; var klon = template.clone(); klon.attr('id', 'line_' + rowId) .insertAfter($('.row').last()) .find('option') .each(function () { $(this).attr('id', $(this).attr('id').replace(/_(\\d*)$/, "_"+rowId)); }) }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="row" id="line_1"> <div class="form-group col-md-2"> <label class="control-label">State</label> <select class="form-control"> <option id="Select_1">Select State</option> <option id="Selangor_1">Selangor</option> <option id="KualaLumpur_1">Kuala Lumpur</option> <option id="Malacca_1">Malacca</option> <option id="Perak_1">Perak</option> <option id="Kedah_1">Kedah</option> </select> </div> </div> <a id="cloneButton" class="btn btn-primary">Add State</a> 

Try this:

var template = $('#line_1').clone();

$('#cloneButton').click(function () {
    var rowId = $('.row').length + 1;
    var klon = template.clone();   
    console.log(klon)       
    klon.attr('id', 'line_' + rowId)
        .insertAfter($('.row').last())
        .find('option')
        .each(function () {
            $(this).attr('id', $(this).attr('id').replace(/_(\d*)$/, "_"+rowId));
        })    

        $("#line_" + rowId).append("<a href='javascript:void(0);' class='remove'>delete</a>")               
});

$(document).on("click", ".remove", function() {
  $(this).closest(".row").remove();
});

You can add a remove button on the row div itself and bind the click event using event delegation.

var template = $('#line_1').clone();

$('#cloneButton').click(function() {
  var rowId = $('.row').length + 1;
  var klon = template.clone();
  klon.attr('id', 'line_' + rowId)
    .insertAfter($('.row').last())
    .find('option')
    .each(function() {
      $(this).attr('id', $(this).attr('id').replace(/_(\d*)$/, "_" + rowId));
    })
});

$(document).on("click", ".remove", function() {
  $(this).closest(".row").remove();
});

HTML:

<div class="row" id="line_1">
  <div class="form-group col-md-2">
    <label class="control-label">State</label>
    <select class="form-control">
      <option id="Select_1">Select State</option>
      <option id="Selangor_1">Selangor</option>
      <option id="KualaLumpur_1">Kuala Lumpur</option>
      <option id="Malacca_1">Malacca</option>
      <option id="Perak_1">Perak</option>
      <option id="Kedah_1">Kedah</option>
    </select>
  </div>
  <input type="button" class="remove" value="remove" />
</div>

<a id="cloneButton" class="btn btn-primary">Add State</a>

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM