Boostrap modal - 一个按钮具有单击和按键事件

Boostrap modal - a button has click and keypress event

本文关键字:单击 事件 按钮 一个 modal Boostrap      更新时间:2023-09-26

我想在引导模式中使用两个事件(display.bs.modal)。它仅适用于点击。 $('id').click('shown.bs.modal, function(e) { $('#regModal').modal('show');}

但它在两个事件中不起作用。

$('id').bind("keypress shown.bs.modal", function (e) {... }

如何在一个按钮中使用两个事件?

我认为您想将侦听器添加到模态本身。

$('#myModal').on('shown.bs.modal', function(e) {
    // hold a reference to the triggering button in case you need it
    var $button = $(e.relatedTarget);
    // handler code here, executed after the modal is shown
});

要触发模态,请使用

$('#myButton').on('click', function() {
    $('#myModal').modal();
});

如果希望给定元素响应不同的事件,则必须单独定义这些事件。

您的示例:

$('id').click('shown.bs.modal, function(e) { $('#regModal').modal('show');}

成为:

var showModal = function(){ $('#regModal').modal('show'); };
$('id').on('shown.bs.modal', showModal());
$('id').on('click', showModal());