简体   繁体   English

引导箱对话框中的表单

[英]Form in Bootbox Dialog

I want to display a Bootbox dialog that includes an "OK" button. 我想显示一个包含“确定”按钮的Bootbox对话框。 When the user clicks the "OK" button, it should POST back, with the ID of the message to confirm that the user has viewed the message. 当用户单击“确定”按钮时,它应该回发,并带有消息的ID,以确认用户已查看消息。 The Bootbox dialog is the form. Bootbox对话框就是表格。 I don't see a way to make the dialog buttons a submit form with hidden fields, which is what I assume is the right way to accomplish my goals. 我没有一种方法可以使对话框按钮成为具有隐藏字段的提交表单,这是我认为是实现目标的正确方法。

Thanks to the helpful comments from Isabel Inc and Mistalis , I have a working solution. 感谢Isabel IncMistalis的有用评论,我找到了一个可行的解决方案。

Notes specific to my implementation: 具体实施说明:

  • The messages contain images that need to be responsive, so I add the appropriate Bootstrap classes to each image 消息中包含需要响应的图像,因此我向每个图像添加了相应的Bootstrap类
  • The messages may have links. 这些消息可能具有链接。 Clicking a link is equivalent to clicking "OK" on the dialog 单击链接等效于在对话框上单击“确定”

And the JavaScript that makes it happen... 以及实现它的JavaScript ...

jQuery(function($, window, bootbox, undefined) {
    var accept = function(callback) {
        $.ajax({
            type: 'POST',
            data: {message_id: 244826},
            success: callback()
        });
    };

    var $message = $('<p><img src="https://www.gravatar.com/avatar/80fa81938a6d1df92cd101d7fe997a71" alt></p><p>Here is a message from <a href="https://stackoverflow.com/users/244826/sonny">Sonny</a>.</p>');
    $message.find('img').addClass('img-responsive center-block');
    $message.find('a').on('click', function(e) {
        var href = $(this).attr('href');
        if (0 === href.length) {
            return;
        }
        e.preventDefault();
        accept(function() { window.location.href = href; });
    });

    bootbox.dialog({
        message: $message,
        closeButton: false,
        buttons: {
            accept: {
                label: 'OK',
                callback: function() {
                    accept(function() { window.location.reload(); });
                }
            }
        }
    });
}(jQuery, window, bootbox));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM