javascript 在 Twitter Bootstrap Modal 中输入按键时触发 onClick 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27151023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:01:47  来源:igfitidea点击:

Trigger onClick event when Enter keypress in a Twitter Bootstrap Modal

javascriptjqueryhtmltwitter-bootstrap-3

提问by ReynierPM

I've a few modals build on top of Twitter Bootstrap Modal, each have at least onesubmit button which is the default action I need to trigger in most cases since the second button is for go back (dismiss the modal), how, on a modal, when I hit enter I can trigger whatever action that submit has? For example right now, as they are button I have something like:

我有一些建立在 Twitter Bootstrap Modal 之上的模态,每个都有至少一个提交按钮,这是我在大多数情况下需要触发的默认操作,因为第二个按钮用于返回(关闭模态),如何,在一个模态,当我按下回车键时,我可以触发提交的任何操作?例如现在,因为它们是按钮,所以我有类似的东西:

$("#btn").on("click", function(e){
    e.preventDefault(); // wait... not send form yet, will be a Ajax call
});

Any help? Example code?

有什么帮助吗?示例代码?

For reference, this is the modal content (I using with Twig as part of a Symfony2 project so don't worry about {{ }}):

作为参考,这是模态内容(我使用 Twig 作为 Symfony2 项目的一部分,所以不用担心{{ }}):

<div class="modal fade " id="addNorma" tabindex="-1" role="dialog" aria-labelledby="addNorma" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">{{ 'boton.cerrar'|trans({}, 'AppBundle') }}</span></button>
                <h4 class="modal-title" id="myModalLabel">{{ 'modal.normas.titulo'|trans({}, 'AppBundle') }}</h4>
            </div>
            <div class="modal-body">
                <form id="buscadorNorma" method="POST">
                    <div class="spacer10"></div>
                    <div class="row">
                        <div class="col-md-5 col-lg-5 col-sm-5 col-xs-5">
                            <label for="codigo_norma">{{ 'modal.normas.codigo'|trans({}, 'AppBundle') }}</label>
                            <input type="text" class="form-control" id="codigo_norma" name="codigo_norma" placeholder="{{ 'modal.normas.codigoPlaceholder'|trans({}, 'AppBundle') }}">
                        </div>
                        <div class="col-md-5 col-lg-5 col-sm-5 col-xs-5 col-md-offset-2">
                            <label for="ano_publicacion">{{ 'modal.normas.anno'|trans({}, 'AppBundle') }}</label>
                            <input type="text" class="form-control" id="ano_publicacion" name="ano_publicacion" placeholder="{{ 'modal.normas.annoPlaceholder'|trans({}, 'AppBundle') }}">
                        </div>
                    </div>
                    <div class="spacer10"></div>
                    <div class="row">
                        <div class="col-md-12">
                            <label for="nombre_norma">{{ 'modal.normas.term'|trans({}, 'AppBundle') }}</label>
                            <input type="text" class="form-control" id="nombre_norma" name="nombre_norma" placeholder="{{ 'modal.normas.termPlaceholder'|trans({}, 'AppBundle') }}">
                        </div>
                    </div>
                    <div class="spacer10"></div>
                    <div class="row">
                        <div class="col-md-12">
                            <label for="procedencia_producto">{{ 'modal.normas.comite'|trans({}, 'AppBundle') }}</label>
                            <div class="form-group">
                                <div>
                                    <select name="comite_tecnico" id="comite_tecnico" class="form-control toSelect2">
                                        <option value="" selected="selected">{{ 'seleccioneOpcion'|trans({}, 'AppBundle') }}</option>
                                        {% for key, item in comiteTecnico %}
                                            <option value="{{ key }}">{{ item.nombre }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="spacer5"></div>
                    <div class="row">
                        <div class="col-md-12">
                            <button type="button" class="btn btn-default pull-right" disabled="disabled" id="btnBuscarNorma"><i class="fa fa-binoculars"></i> {{ 'boton.buscar'|trans({}, 'AppBundle') }}</button>
                        </div>
                    </div>
                </form>
                <div class="spacer10"></div>
                <table  class="table table-hover table-condensed" id="resultadoNorma" style="display: none">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="toggleCheckboxNorma" name="toggleCheckboxNorma" /></th>
                            <th>{{ 'columnas.normas.no'|trans({}, 'AppBundle') }}</th>
                            <th>{{ 'columnas.normas.norma'|trans({}, 'AppBundle') }}</th>
                            <th>{{ 'columnas.normas.anno'|trans({}, 'AppBundle') }}</th>
                            <th>{{ 'columnas.normas.comite'|trans({}, 'AppBundle') }}</th>
                        </tr>
                    </thead>
                    <tbody id="resultadoNormaBody"></tbody>
                </table>
                <div class="alert alert-danger" role="alert" style="display: none;" id="sinResultadosBuscarNormas">
                    {{ 'mensajes.msgNoEncontrado'|trans({}, 'AppBundle') }}
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">{{ 'boton.regresar'|trans({}, 'AppBundle') }}</button>
                <button type="submit" class="btn btn-primary" id="btnAplicarNorma" disabled="disabled"><i class="fa fa-save"></i> {{ 'boton.aplicar'|trans({}, 'AppBundle') }}</button>
            </div>
        </div>
    </div>
</div>

And that's the code I trigger when I click on #btnBuscarNorma:

这就是我点击时触发的代码#btnBuscarNorma

$('button#btnBuscarNorma').on('click', function (ev) {
    ev.preventDefault();
    $.post(Routing.generate('filtrarNormas'), $('#buscadorNorma').serialize(), 'json')
        .done(function (data, textStatus, jqXHR) {
            $('#resultadoNorma').toggle(!!data.entities.length);
            $("#sinResultadosBuscarNormas").toggle(!data.entities.length);

            if (data.entities.length > 0) {
                var $html = '';
                data.entities.forEach(function (value, index, array) {
                    $html += '<tr>';
                    $html += '<td><input type="checkbox" id="' + value.id + '" value="' + value.id + '"></td>';
                    $html += '<td>' + value.codigo + '</td>';
                    $html += '<td>' + value.norma + '</td>';
                    $html += '<td>' + value.anno + '</td>';
                    $html += '<td>' + value.comiteTecnico + '</td>';
                    $html += '</tr>';
                });

                $("table tbody#resultadoNormaBody").html($html);
                marcarTodosCheck('#toggleCheckboxNorma', '#resultadoNormaBody');
            }
        });
});

The idea is Enterkey will trigger #btnBuscarNormaclick event and not #btnAplicarNormaclick event.

这个想法是Enter关键将触发#btnBuscarNorma点击事件而不是#btnAplicarNorma点击事件。

回答by Mathieu Labrie Parent

Bind this event when you open a modal.

打开模态时绑定此事件。

//to prevent multiple binding
$(document).unbind("keyup").keyup(function(e){ 
    var code = e.which; // recommended to use e.which, it's normalized across browsers
    if(code==13)
    {
        $("#btn").click();
    }
});

回答by Máxima Alekz

When modal is opened inclass is active on it. So you can get in JQuery:

当模态课堂上打开时处于活动状态。所以你可以进入JQuery:

$("#addNorma.in").on("keydown",function(e){
    if(e.which == 13){
        //Do something.
    }
});

回答by Qurben

You can listen for a form submit and prevent it.

您可以监听表单提交并阻止它。

$('#myform').on('submit', function (e) {
  e.preventDefault();
});

This way you can make sure the form is never submitted. You can also start the Ajax call when the submit function is called.

通过这种方式,您可以确保永远不会提交表单。您还可以在调用提交函数时启动 Ajax 调用。

A normal button <button>also doesn't submit the form.

普通按钮<button>也不会提交表单。