How to perform an input operation using a checkbox

0

I need your help as I could perform an operation (in this case a multiplication by * 0.13) in the 2 inputs (item_1 and item_2) by means of a checkbox, that is when you enter a quantity in the inputs and press the checkbox, the operation and the value of the inputs are modified with the new value.

<!DOCTYPE html>
<html>
 <head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 </head>
 <body>
  <br />
  <div class="container">
   <h3 align="center"></h3>
   <br />
   <h4 align="center">Enter Item </h4>
   <br />
   <form method="post" id="insert_form">
    <div class="table-repsonsive">
     <span id="error"></span>
     <table class="table table-bordered" id="item_table">
      <tr>
       <th>Enter Item_1</th>
       <th>Enter Item_2</th>
       <th>multiplicacion</th>
      



       <th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
      </tr>
     </table>
     <div align="center">
      <input type="submit" name="submit" class="btn btn-info" value="Insert" />
     </div>
    </div>
   </form>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){
 
 $(document).on('click', '.add', function(){
  var html = '';
  html += '<tr>';
  html += '<td><input type="text" name="item_1[]" class="form-control item_1" /></td>';
  html += '<td><input type="text" name="item_2[]" class="form-control item_2" /></td>';
    html += '<td><input type="checkbox" name="item3[]" class="form-control item3" /></td>';
     

  html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
  $('#item_table').append(html);
 });
 
 $(document).on('click', '.remove', function(){
  $(this).closest('tr').remove();
 });
 
 $('#insert_form').on('submit', function(event){
  event.preventDefault();
  var error = '';
  $('.item_1').each(function(){
   var count = 1;
   if($(this).val() == '')
   {
    error += "<p>Enter Item 1 at "+count+" Row</p>";
    return false;
   }
   count = count + 1;
  });
  
  $('.item_2').each(function(){
   var count = 1;
   if($(this).val() == '')
   {
    error += "<p>Enter Item 2 at "+count+" Row</p>";
    return false;
   }
   count = count + 1;
  });
  
  var form_data = $(this).serialize();
  if(error == '')
  {
   $.ajax({
    url:"insert.php",
    method:"POST",
    data:form_data,
    success:function(data)
    {
     if(data == 'ok')
     {
      $('#item_table').find("tr:gt(0)").remove();
      $('#error').html('<div class="alert alert-success">Item Details Saved</div>');
     }
    }
   });
  }
  else
  {
   $('#error').html('<div class="alert alert-danger">'+error+'</div>');
  }
 });
 
});
</script>
    
asked by outsider 29.11.2018 в 04:47
source

1 answer

1

Use the event change of the checkbox to know when it changed:

$(document).ready(function(){

  $(document).on('click', '.add', function(){
    var html = '';
    html += '<tr>';
    html += '<td><input type="text" name="item_1[]" class="form-control item_1" /></td>';
    html += '<td><input type="text" name="item_2[]" class="form-control item_2" /></td>';
    html += '<td><input type="checkbox" name="item3[]" class="form-control item3" /></td>';
    html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
    $('#item_table').append(html);
  });
 
  $(document).on('click', '.remove', function(){
    $(this).closest('tr').remove();
  });
  
  $('#item_table').on('change', 'input[type=checkbox]', function(e) {
    var row = $(this).closest('tr');
    row.find('input[type=text]')
       .each(function(index, element) {
          if (e.target.checked) {
            element.value *= 0.13;
          }
          else {
            element.value /= 0.13;
          }
       });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
<div class="container">
  <h3 align="center"></h3>
  <br />
  <h4 align="center">Enter Item </h4>
  <br />
  <form method="post" id="insert_form">
  <div class="table-repsonsive">
   <span id="error"></span>
   <table class="table table-bordered" id="item_table">
    <tr>
     <th>Enter Item_1</th>
     <th>Enter Item_2</th>
     <th>multiplicacion</th>

     <th>
      <button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button>
     </th>
    </tr>
   </table>
  </div>
  </form>
</div>
    
answered by 29.11.2018 / 05:27
source