J'ai codé this, mais le bouton Supprimer ne fonctionne pas. Et je n'ai aucune erreur dans la console ..

var counter = 0;    
var dataList = document.getElementById('materials');
    
    var jsonOptions = [{
                        "product": "11111",
                        "description": "description 1"
                      }, {
                        "product": "22222",
                        "description": "description 2"
                      }, {
                        "product": "33333",
                        "description": "description 3"
                      }, {
                        "product": "44444",
                        "description": "description 4"
                      }, {
                        "product": "55555",
                        "description": "description 5"
                      }]; 

    jsonOptions.forEach(function(item) {
                    
      var option = document.createElement('option');
      
      option.value = item.description;
      option.text = item.description;
      option.setAttribute('data-product', item.product);
      dataList.appendChild(option);
    });

$('#bookForm')
    // Add button click handler
    .on('change', 'input[id^="ajax"]', function() {
    
        var description = $(this).val();
        var product = $('#dataList > option[value="' + description + '"]').data('product');
        $('input[name=product-'+ $(this).attr("name").replace("description-", "") + ']').val(product);
});

    
$('#bookForm')
    // Add button click handler
    .on('click', '.addButton', function() {
        counter++;
        var $template = $('#bookTemplate'),
            $clone    = $template
                            .clone()
                            .removeClass('hide')
                            .removeAttr('id')
                            .attr('data-book-index', counter)
                            .insertBefore($template);

        // Update the name attributes
        $clone
            .find('[name="description"]').attr('name', 'description-' + counter).end()
            .find('[name="product"]').attr('name', 'product-' + counter).end();
    })

    // Remove button click handler
    .on('click', '.removeButton', function() {
        var $row  = $(this).parents('.form-group'),
            index = $row.attr('data-book-index');

        // Remove element containing the fields
        $row.remove();
    });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
         
    	<div id="bookForm" class="form-group">
                      <label for="inputName" class="col-md-2 col-sm-2 control-label">Υλικό / Ποσότητα</label>
                      <div class="col-md-3 col-sm-3">
                         <input type="text" id="ajax2" list="materials" class="form-control" placeholder="Υλικό" name="description-0">
                         <datalist id="materials"></datalist>
                      </div>
                     
                      <div class="col-md-3 col-sm-3">
                        <input type="number" class="form-control" name="product-0" placeholder="Ποσότητα" required="">
                      </div>
                      
                      <div class="col-xs-1">
                            <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
                        </div>
                    </div>
                    
                    <div id="bookTemplate" class="form-group hide">
                      
                        <div class="col-md-3 col-sm-3 col-md-offset-2 col-sm-offset-2">
                           <input type="text" id="ajax2" list="materials" class="form-control" placeholder="Υλικό" name="description">
                           <datalist id="materials"></datalist>
                        </div>

                        <div class="col-md-3 col-sm-3">
                          <input type="number" class="form-control" name="product" placeholder="Ποσότητα" required="">
                        </div>

                       <div class="col-xs-1">
                          <button type="button" class="btn btn-default removeButton"><i class="fa fa-minus"></i></button>
                      </div> 
                    </div>
    
    
           
   </div>
</div>
0
yaylitzis 8 mars 2016 à 10:42

3 réponses

Meilleure réponse

Le problème est que vous devez sélectionner la classe .removeButton à l'intérieur #bookForm, mais en réalité, .removeButton est en dehors de #bookForm

jsFiddle

$('#bookForm')
    // Add button click handler
    .on('click', '.addButton', function() {
        counter++;
        var $template = $('#bookTemplate'),
            $clone    = $template
                            .clone()
                            .removeClass('hide')
                            .removeAttr('id')
                            .attr('data-book-index', counter)
                            .insertBefore($template);

        // Update the name attributes
        $clone
            .find('[name="description"]').attr('name', 'description-' + counter).end()
            .find('[name="product"]').attr('name', 'product-' + counter).end();
    });

    // Remove button click handler
    $("#bookForm").parent().on('click', '.removeButton', function() {
        var $row  = $(this).parents('.form-group'),
            index = $row.attr('data-book-index');

        // Remove element containing the fields
        $row.remove();
    });
1
Shayan 8 mars 2016 à 07:48
var counter = 0;    
var dataList = document.getElementById('materials');
    
    var jsonOptions = [{
                        "product": "11111",
                        "description": "description 1"
                      }, {
                        "product": "22222",
                        "description": "description 2"
                      }, {
                        "product": "33333",
                        "description": "description 3"
                      }, {
                        "product": "44444",
                        "description": "description 4"
                      }, {
                        "product": "55555",
                        "description": "description 5"
                      }]; 

    jsonOptions.forEach(function(item) {
                    
      var option = document.createElement('option');
      
      option.value = item.description;
      option.text = item.description;
      option.setAttribute('data-product', item.product);
      dataList.appendChild(option);
    });

$('#bookForm')
    // Add button click handler
    .on('change', 'input[id^="ajax"]', function() {
    
        var description = $(this).val();
        var product = $('#dataList > option[value="' + description + '"]').data('product');
        $('input[name=product-'+ $(this).attr("name").replace("description-", "") + ']').val(product);
});

    
$('#bookForm')
    // Add button click handler
    .on('click', '.addButton', function() {
        counter++;
        var $template = $('#bookTemplate'),
            $clone    = $template
                            .clone()
                            .removeClass('hide')
                            .removeAttr('id')
                            .attr('data-book-index', counter)
                            .insertBefore($template);

        // Update the name attributes
        $clone
            .find('[name="description"]').attr('name', 'description-' + counter).end()
            .find('[name="product"]').attr('name', 'product-' + counter).end();
    });


$(document).on('click', '.removeButton', function() {
    var $row  = $(this).parents('.form-group'),
        index = $row.attr('data-book-index');

    // Remove element containing the fields
    $row.remove();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
         
    	<div id="bookForm" class="form-group">
                      <label for="inputName" class="col-md-2 col-sm-2 control-label">Υλικό / Ποσότητα</label>
                      <div class="col-md-3 col-sm-3">
                         <input type="text" id="ajax2" list="materials" class="form-control" placeholder="Υλικό" name="description-0">
                         <datalist id="materials"></datalist>
                      </div>
                     
                      <div class="col-md-3 col-sm-3">
                        <input type="number" class="form-control" name="product-0" placeholder="Ποσότητα" required="">
                      </div>
                      
                      <div class="col-xs-1">
                            <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
                        </div>
                    </div>
                    
                    <div id="bookTemplate" class="form-group hide">
                      
                        <div class="col-md-3 col-sm-3 col-md-offset-2 col-sm-offset-2">
                           <input type="text" id="ajax2" list="materials" class="form-control" placeholder="Υλικό" name="description">
                           <datalist id="materials"></datalist>
                        </div>

                        <div class="col-md-3 col-sm-3">
                          <input type="number" class="form-control" name="product" placeholder="Ποσότητα" required="">
                        </div>

                       <div class="col-xs-1">
                          <button type="button" class="btn btn-default removeButton"><i class="fa fa-minus"></i></button>
                      </div> 
                    </div>
    
    
           
   </div>
</div>
0
Punit Gajjar 8 mars 2016 à 07:53

Vous devez définir un écouteur pour tous removeButton;

function removeButtonListener(){

    $('.removeButton').on('click', function() {
        var $row  = $(this).parents('.form-group'),
            index = $row.attr('data-book-index');
        // Remove element containing the fields
        $row.remove();
    });
}

Et vous devez l'appeler lorsque vous ajoutez une nouvelle ligne;

$('#bookForm').on('click', '.addButton', function() {
        counter++;
        var $template = $('#bookTemplate'),
            $clone    = $template
                            .clone()
                            .removeClass('hide')
                            .removeAttr('id')
                            .attr('data-book-index', counter)
                            .insertBefore($template);

        // Update the name attributes
        $clone
            .find('[name="description"]').attr('name', 'description-' + counter).end()
            .find('[name="product"]').attr('name', 'product-' + counter).end();

        removeButtonListener(); //call remove listener
    });
0
hurricane 8 mars 2016 à 07:51