How to emulate the enter key in jquery? [duplicate]

0

I would like to emulate the enter key since I am generating inputs dynamically, at this moment when you press the button add product or give enter it generates an input and can even be deleted, but I would like my default script to generate one by default at the beginning, I already managed to include my inputs in document ready before, but because my checkbox was not working correctly, that's why I'm looking for this alternative because I can not find the function once at the beginning.

<script>



$(document).ready(function() {
    var max_fields      = 10;
    var wrapper         = $(".container1");
    var add_button      = $(".add_form_field");

     var selectproductos = "<?php $sql = "Select producto from productos"; $query = $db->prepare($sql);
    $query->execute();

       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['producto'].'</option>';
       }
     ?>";

            var selectunidades = "<?php $sql = "Select unidad from unidades";

    $query = $db->prepare($sql);
    $query->execute();
       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['unidad'].'</option>';
       }
     ?> ";




    var x = 1;
    $(add_button).click(function(e){
        e.preventDefault();
        if(x < max_fields){
            x++;
                var selectproductos = "<?php $sql = "Select producto from productos"; $query = $db->prepare($sql);
    $query->execute();

       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['producto'].'</option>';
       }
     ?>";

            var selectunidades = "<?php $sql = "Select unidad from unidades";

    $query = $db->prepare($sql);
    $query->execute();
       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['unidad'].'</option>';
       }
     ?> ";

                 $(wrapper).children('table').append('<tr>   <td> <select required name="productos[]"><option value="">Selecciona un Producto</option>'+selectproductos+'</select><td><select required name ="unidad[]"><option value="">Selecciona una unidad</option>'+selectunidades+'</select></td><td><input type="text" class="inputancho" name="cantidad[]" placeholder="cantidad" required="required"/></td><td><input type="date"  name="fecha_de_embarque[]" required="required"/></td>  <td> <textarea  rows="2" cols="30" name="notas[]" id="notas" maxlength="255"></textarea> </td> <td>    <input type="hidden" class="checkbox_handler" name="etiquetado[]" value="No" /><input type="checkbox" name="etiquetado_ck[]" value="Si" />   </td><td><a href="#" class="delete">Eliminar</a></<td></tr>'); //add input box




$(document).on("change", "input[type='checkbox']", function() {
    var checkbox_val = ( this.checked ) ? 'Si' : 'No';
    $(this).siblings('input.checkbox_handler').val(checkbox_val);
});

        }
  else
  {
  alert('You Reached the limits')
  }
    });

   $(wrapper).on("click",".delete", function(e){
    e.preventDefault(); $(this).parent('td').parent('tr').remove(); x--;
})






});



$(document).keypress(function(e) {
    if(e.which == 13) {

        $("#botonagregarproducto").click();

    }
});





</script>
    
asked by Daniel Treviño 01.11.2017 в 21:13
source

1 answer

0

If you want to generate a input by default, that is when you start the page, you could do this:

$(document).ready(function (){
    //Tú código
    //       ...

    //Dices que al presionar un botón, se genera un input correctamente,
    //Entonces ejecuta esto al final de lo que ejecutas cuando se carga la página
    $("#botonagregarproducto").click();
});

The ready method of jQuery , allows you to execute a function when the site is already loaded, you currently already execute a function, but if you add $("#botonagregarproducto").click(); as the last instruction that is executed in the function, then you should "emulate" a click on your button, and then the function would be executed that allows you to add a input , which it would do.

I hope and it works for you.

    
answered by 02.11.2017 в 07:40