Show Modal Popup when giving enter in a textbox

1

I'm trying to show a modal window by giving enter on a textbox , I had the following code to do it but it does not work for me:

$("#txt_codigoCat").on("keydown", function (event) {
    if (event.which == 13) {
        $('#popupBusquedaCliente').modal('show');
    }           
});

I add the code I use for the modal window:

     <!-- Modal Escenario-->
<div class="modal fade" id="popupBusquedaCliente" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Cerrar</span></button>
        <h4 class="modal-title" id="myModalLabel">Busqueda de Familia</h4>
      </div>
      <div id="BusquedaCliente" class="modal-body">
        <form role="form">
          <div class="form-group">
            <label for="stock_bodega">Busqueda por:</label>
            <select class="form-control" style="width: 40%" id="stock_bodega">
              <option>C&oacute;digo</option>
              <option>Nombre</option>
              <option>Estado</option>
            </select>
            <label for="texto_buscar">Texto a Buscar:</label>
            <input type="text" class="form-control" id="texto_buscar" autocomplete="off" style="text-transform: uppercase">
          </div>

          <div style="position: relative; overflow: auto; width: 100%; height: 200px;" class="dataTables_scrollBody">
            <table style="width: 100%;" id="busqueda_cliente" class="display nowrap dataTable no-footer" cellspacing="0" width="100">
              <thead>
                <tr>
                  <th>Codigo</th>
                  <th>Nombre</th>
                  <th>Estado</th>
                  <th>Categoria</th>
                  <th>CatNombre</th>
                </tr>
              </thead>

            </table>
          </div>
        </form>
      </div>
      @* Load datatable css *@
      <link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" /> @* Load datatable js *@ @section Scripts{
      <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
      <script>
        $(document).ready(function() {
          $('#busqueda_cliente').DataTable({
            "bSort": false,
            "ajax": {
              "url": '@Url.Action("Todos", "venmanfamilia")',
              "type": "GET",
              "datatype": "json"
            },
            "columns": [{
              "data": "Codigo"
            }, {
              "data": "Nombre"
            }, {
              "data": "Estado"
            }, {
              "data": "Categoria"
            }, {
              "data": "CatNombre"
            }],
            filter: false,
            "paging": false,
            "ordering": false,
            "info": false,
            language: {
              paginate: {
                first: "Primero",
                previous: "Anterior",
                next: "Siguiente",
                last: "Ultimo"
              }
            }
          });
        });

      </script>
      }
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Seleccionar</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal">Cerrar</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal Escenario-->
    
asked by Ricardo España 19.12.2016 в 16:00
source

1 answer

0

If your code does not work, it may be a problem with the .js files you are using or the order in which you add the references. For your code to work, you only need these three references:

  • Jquery
  • Bootstrap (js)
  • Bootstrap (css)
  • Try these references:

    <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
    

    It is important that you follow the order, first you must put jquery.min.js and then bootstrap.min.js , since boostrap depends on jquery.

    Another thing that you must validate is to remove any other references to .js files that you have in your code, because they may be causing conflicts.

    You can download the files locally and put them in your project:

    Bootstrap (css)

    Jquery

    Bootstrap (js)

    Finally add the example so you can validate, greetings.

    $("#txt_codigoCat").on("keydown", function (event) {
        if (event.which == 13) {
            $('#popupBusquedaCliente').modal('show');
        }           
    });
    <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
    
    <input type="text" id="txt_codigoCat"/>
    <!-- Modal Escenario-->
    <div class="modal fade" id="popupBusquedaCliente" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Cerrar</span></button>
            <h4 class="modal-title" id="myModalLabel">Busqueda de Familia</h4>
          </div>
          <div id="BusquedaCliente" class="modal-body">
            <form role="form">
              <div class="form-group">
                <label for="stock_bodega">Busqueda por:</label>
                <select class="form-control" style="width: 40%" id="stock_bodega">
                  <option>C&oacute;digo</option>
                  <option>Nombre</option>
                  <option>Estado</option>
                </select>
                <label for="texto_buscar">Texto a Buscar:</label>
                <input type="text" class="form-control" id="texto_buscar" autocomplete="off" style="text-transform: uppercase">
              </div>
    
              <div style="position: relative; overflow: auto; width: 100%; height: 200px;" class="dataTables_scrollBody">
                <table style="width: 100%;" id="busqueda_cliente" class="display nowrap dataTable no-footer" cellspacing="0" width="100">
                  <thead>
                    <tr>
                      <th>Codigo</th>
                      <th>Nombre</th>
                      <th>Estado</th>
                      <th>Categoria</th>
                      <th>CatNombre</th>
                    </tr>
                  </thead>
    
                </table>
              </div>
            </form>
          </div>
          @* Load datatable css *@
          <link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" /> @* Load datatable js *@ @section Scripts{
          <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
          <script>
            $(document).ready(function() {
              $('#busqueda_cliente').DataTable({
                "bSort": false,
                "ajax": {
                  "url": '@Url.Action("Todos", "venmanfamilia")',
                  "type": "GET",
                  "datatype": "json"
                },
                "columns": [{
                  "data": "Codigo"
                }, {
                  "data": "Nombre"
                }, {
                  "data": "Estado"
                }, {
                  "data": "Categoria"
                }, {
                  "data": "CatNombre"
                }],
                filter: false,
                "paging": false,
                "ordering": false,
                "info": false,
                language: {
                  paginate: {
                    first: "Primero",
                    previous: "Anterior",
                    next: "Siguiente",
                    last: "Ultimo"
                  }
                }
              });
            });
    
          </script>
          }
          <div class="modal-footer">
            <button type="button" class="btn btn-primary">Seleccionar</button>
            <button type="button" class="btn btn-primary" data-dismiss="modal">Cerrar</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Modal Escenario-->
        
    answered by 19.12.2016 / 19:08
    source