如何避免Twitter Bootstrap Modal组件中的ENTER键行为

时间:2014-12-23 04:26:09

标签: javascript jquery html twitter-bootstrap twitter

我有一个用于Twitter Bootstrap模式的HTML:

<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="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>
                <table  class="table table-hover table-condensed" id="resultadoNorma" style="display: none">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="toggleCheckboxNormaModal" name="toggleCheckboxNormaModal" /></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>
            <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><i class="fa fa-save"></i> {{ 'boton.aplicar'|trans({}, 'AppBundle') }}</button>
            </div>
        </div>
    </div>
</div>

这是处理#btnBuscarNorma

的代码
$('#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);
        }
    });
});

这个用于处理#btnAplicarNorma按钮:

$('#btnAplicarNorma').on('click', function (ev) {
    var $tr_to_append = $('#resultadoNormaBody').find('tr.copyMe'),
        has_tr = $("#normaBody").find('tr');

    $('#tablaNorma').removeClass('hidden');
    $('#alertSinNorma').addClass('hidden');
    $('#toggleCheckboxNormaModal').prop('checked', false);

    if ($tr_to_append.length) {
        $tr_to_append.find('input[type=checkbox]').prop('checked', false);
        $tr_to_append.removeClass('copyMe');
        $(this).prop('disabled', true);

        var clonedRows = $("#normaBody").find("tr");
        $.each($tr_to_append, function (i, v) {
            var added = false;
            var currentRowHtml = $(v).html();
            $.each(clonedRows, function (i, cRow) {
                var clonedRowHtml = $(cRow).html();
                if (currentRowHtml == clonedRowHtml) {
                    added = true;
                }
            });

            if (!added) {
                $(v).clone().appendTo('#normaBody').removeClass('copyMe');
            }
        });
    }

    $("#btnGuardarPasoTresAgregarProducto").prop('disabled', false);
    $('#addNorma').modal('hide');

    if ($('#toggleCheckboxNormaModal').is(":checked")) {
        $('#toggleCheckboxNormaModal').prop("checked", false);
    }
});

我遇到的问题是,只要我按下enter键,模式就会关闭,我会重定向到某个网址,为什么?我为什么要运行这个问题?避免这种行为的任何解决方法?

2 个答案:

答案 0 :(得分:1)

您可以使用以下命令禁止表单上的提交事件:

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

答案 1 :(得分:0)

将模式的背景值设置为静态

<div class="modal fade " id="addNorma" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog" aria-labelledby="addNorma" aria-hidden="true">

我希望这会有所帮助......