0

クリック イベントで、多くのリンク、ボタン、または入力に確認ボックスを追加しようとしています。

次の理由により、location.href、submit()、またはその他の特定の機能を使用できません。

  • たとえば、location.href は送信ボタンでは機能しません。
  • たとえば、location.href は他のバインドされたハンドラーをトリガーしません。

したがって、私が使用する必要があるのは、理論的にすべてのハンドラーとネイティブ アクションを実行する trigger() 関数です。「難しい」部分は、確認ボックスをポップするハンドラーを除いて、すべてのハンドラーを実行することです。

これが私のコードです:

$('a, button, input[type="submit"]').each(function() {

    var oButton = $(this);

    oButton.on('click.pending', function(oEvent) {
        console.log('click !');
        oEvent.preventDefault();

        var oDialog = $('<div class="dialog-example">[Question] ?</div>').dialog({
            buttons: {
                Cancel: function() {
                    console.log('cancelled !');
                    // Nothing to do
                    oDialog.dialog('close');
                },
                Ok: function() {
                    console.log('confirmed !');
                    // Trigger the rest of the handlers AND the native action, BUT not this one, so this dialog is not used
                    // Problem : nothing happens here
                    oButton.trigger('click.confirmed');
                    oDialog.dialog('close');
                }
            }
        });
    });

});

前もって感謝します !;)

4

1 に答える 1

0

試してみてください:

oButton.off('click.pending').trigger('click').get(0).click();

デモ

于 2013-09-26T08:41:44.747 に答える