Get the value of a checkbox and show it in input text js or jquery

0

Good day how you could get the value of a checkbox and then show it in an input text.

   <form class="form-horizontal" method="post" enctype="multipart/form-data" id="addproduct" action="index.php?view=addproduct" role="form">


  <div class="form-group">

    <label for="inputEmail1" class="col-lg-2 control-label">Fiscal</label>
    <div class="col-md-6">
          <div class="form-check">
               <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" value="2" name="fiscal" id="fiscal">
                    Fiscal
                </label>  
          </div>

      <input type="text" name="id_fiscal" class="form-control" id="id_fiscal">
    </div>
  </div>


  <div class="form-group">
    <label for="inputEmail1" class="col-lg-2 control-label">Existencias Fiscal:</label>
    <div class="col-md-6">
      <input type="text" name="fiscal" class="form-control" id="fiscal" placeholder="Existencias Fiscales">
    </div>
  </div>


  <div class="form-group">

    <label for="inputEmail1" class="col-lg-2 control-label">Nacional</label>
    <div class="col-md-6">
          <div class="form-check">
               <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" value="1" name="nacional" id="nacional">
                    Nacional
                </label>  
          </div>

      <input type="text" name="id_nacional" class="form-control" id="id_nacional">
    </div>
  </div>

  <div class="form-group">
    <label for="inputEmail1" class="col-lg-2 control-label">Existencias Nacional:</label>
    <div class="col-md-6">
      <input type="text" name="nacional" class="form-control" id="nacional" placeholder="Existencias Nacionales">
    </div>
  </div>

<div class="form-group">
    <div class="col-lg-offset-2 col-lg-10">
      <button type="submit" class="btn btn-primary">Agregar Producto</button>
    </div>
  </div>

I have 2 checkbook, and I require when this activated or selected shows me the value in my input text of id_fiscal (2) and id_nacional (1).

    
asked by Ever 01.06.2017 в 19:18
source

2 answers

1

One option would be to listen to the event change of the checkbox and then validate when checkeado and add the value to the Text , taking into consideration Do not repeat the Id for more than one element, remember es un Identificador único

For the example I removed the elements with repeated id

$(document).on('change','input[type="checkbox"]' ,function(e) {
    if(this.id=="fiscal") {
        if(this.checked) $('#id_fiscal').val(this.value);
        else $('#id_fiscal').val("");
    }
    if(this.id=="nacional") {
        if(this.checked) $('#id_nacional').val(this.value);
        else $('#id_nacional').val("");
    }
});

/* Escuchando por Separado*/
/* 
$(document).on('change','#fiscal' ,function(e) {
    if(this.checked) $('#id_fiscal').val(this.value);
    else $('#id_fiscal').val("");
});

$(document).on('change','#nacional' ,function(e) {
    if(this.checked) $('#id_nacional').val(this.value);
    else $('#id_nacional').val("");
});
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="inputEmail1" class="col-lg-2 control-label">Fiscal</label>
    <div class="col-md-6">
          <div class="form-check">
               <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" value="2" name="fiscal" id="fiscal">
                    Fiscal
                </label>  
          </div>
      <input type="text" name="id_fiscal" class="form-control" id="id_fiscal">
    </div>
    
<label for="inputEmail1" class="col-lg-2 control-label">Nacional</label>
    <div class="col-md-6">
          <div class="form-check">
               <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" value="1" name="nacional" id="nacional">
                    Nacional
                </label>  
          </div>

      <input type="text" name="id_nacional" class="form-control" id="id_nacional">
    </div>
    
answered by 01.06.2017 / 19:37
source
0

This is pure javascript:

function uncheck(){
checkbox2.onclick = function(){
if(document.getElementById("vehiculo").checked==true){
        document.getElementById("vehiculo").value=55;
        document.getElementById("costo").value=document.getElementById("vehiculo").value;
    }else{
        document.getElementById("costo").value="";
    }
if(document.getElementById("moto").checked==true){
        document.getElementById("moto").value=35;
        document.getElementById("costo").value=document.getElementById("moto").value;
    }else{
        document.getElementById("costo").value="";
    }
}
}

    
answered by 26.11.2018 в 18:47