javascript - 在 Twitter Bootstrap 模态中输入按键时触发 onClick 事件

标签 javascript jquery html twitter-bootstrap-3

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

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

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

作为引用,这是模态内容(我将 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>

这就是我点击#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');
            }
        });
});

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

最佳答案

打开模态框时绑定(bind)此事件。

//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();
    }
});

关于javascript - 在 Twitter Bootstrap 模态中输入按键时触发 onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151023/

相关文章:

javascript - 序列化 JS 对象以供离线使用

Javascript onblur 事件警报框不会消失

javascript - 下划线模板和其他html文件如何绑定(bind)?

jquery - 在父级中选择多个类

html - IMG 与自动换行问题对齐?

javascript - 服务器端表单处理和检索客户端的服务器响应

javascript - 尽管有控制台打印,AngularJS ng-if 表达式仍未评估

javascript - 如何在事件上设置断点?

javascript - 折叠动画效果不起作用(改为溶解) - Fiddle inside

html - 使用 CSS3 剪切内容