How to value an input that is inside a form? with jQuery

1

Good I wanted to know how to print a value in an input that is in a Form within a Modal with jQuery, if I put the input out of the Form it prints it normally but when I put the input into the Form it does not.

This is the function I use to bring the value I want to print (works correctly)

 // ABRIR EL MODAL
   $('#btnAddClase').click(function(){
      ultimocontador();
      $('#modalclase').modal('show');
      $('#modalclase').find('.modal-title').text('Nueva Clase');
    });
 
 // FUNCION CONTADOR
 function ultimocontador() {
      $.ajax({
      type: 'ajax',
      url: '<?php echo base_url() ?   >mantenimiento/logistica/clase/ultimocontador',
       async: false,
       dataType: 'json',
      success: function(data){
      var  html = "";
      var i ;
      var con = 0 ; 
       var suma = 0;
      for(i=0; i<data.length; i++){
      con = data[i].contador;
      suma = parseInt(con)+ 1;
       html += suma;
      }
$("#contador1").val(html);   // <-  QUIERO IMPRIMIR   LA VARIABLE HTML EN EL INPUT DENTRO DEL FORM
} ,
    error: function(){
     swal({
    title: "Error!",
    text: "Ocurrio un error!",
    icon: "error",
    button: "Aceptar",
    });
    }
});
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- BOTON PARA ABRIR EL MODAL -->
 <div class="box-header">
    <button type="button" class="btn btn-success "          id="btnAddClase">
      Crear Nuevo
    </button>
  </div>

<!-- MODAL -->
<div class="modal fade" id="modalclase" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="font-family: verdana;font-size: 12px;">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
     <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel"><b></b></h5>
      </div>
      <div class="modal-body">

        <form action="" method="POST" id="form_clase">
          <input type="hidden" name="txtIdClase" value="0">
            <input type="text" class="form-control" id="contador1" name="contador" required="true"> <!--AQUI QUIERO IMPRIMIRLO-->
             <div class="form-group">
            <label for="">Contador<span style="color: red;">*</span></label>
          </div>
          <div class="form-group">
            <label for="">Digitar Nueva Clase <span style="color: red;">*</span></label>
            <input type="text" class="form-control" id="nClase" name="nClase" required="true">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cerrar</button>
        <button type="button" id="btnsaveClase" class="btn btn-success">Guardar</button>
      </div>
    </div>
  </div>
</div>

Could someone help me? please

    
asked by Luis 09.04.2018 в 19:50
source

3 answers

0

You can try giving a value to your variable html , because initially it does not bring data, for when you send it to call you show the assigned value. Ex:

var  html = "nuevo valor";
    
answered by 10.04.2018 в 05:19
0
$(".form_clase").find("#contador1").val(variable);

Have you already checked that if your information really returns to you, your variable?

    
answered by 10.04.2018 в 06:18
0

Thanks to all my mistake was to open the modal after calling the function

  // ERROR 
  
 $('#btnAddClase').click(function(){
      ultimocontador();  <-- LLAMA A LA FUNCION
      $('#modalclase').modal('show');  <-- SE ABRE EL MODAL
      $('#modalclase').find('.modal-title').text('Nueva Clase');
    });

The way to do it was to open the modal first and then call the function

 $('#btnAddClase').click(function(){
      $('#modalclase').modal('show');  <-- SE ABRE EL MODAL
      ultimocontador();  <-- LLAMA A LA FUNCION
      $('#modalclase').find('.modal-title').text('Nueva Clase');
    });
    
answered by 15.04.2018 в 17:11