添加按钮到jquery ui对话框

Sha*_*hin 15 jquery jquery-ui jquery-ui-dialog

我无法在此jquery ui对话框中添加按钮.如果可能的话请举个例子.谢谢.

<script type="text/javascript">
    $(document).ready(function () {
        //setup new person dialog
        $('#dialog2').dialog({
            autoResize: true,
            show: "clip",
            hide: "clip",
            height: 'auto',
            width: '1000',
            autoOpen: false,
            modal: true,
            position: 'top',
            draggable: false,
            title: "?????? ???????",
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });

        $('#viewfaktor').dialog({
            autoResize: true,
            show: "clip",
            hide: "clip",
            height: 'auto',
            width: '1000',
            autoOpen: false,
            modal: true,
            position: 'top',
            draggable: true,
            title: "?????? ???? ???",
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });


        $('#msgBox').dialog({


            autoResize: true,
            show: "clip",
            hide: "clip",
            height: 'auto',
            width: 'auto',
            autoOpen: false,
            modal: true,
            position: 'center',
            draggable: false,



            open: function (type, data) {
                $(this).parent().appendTo("form");
            }


        });



    });

    function showDialog(id) {
        $('#' + id).dialog("open");
    }

    function closeDialog(id) {
        $('#' + id).dialog("destroy");
    }



</script>
Run Code Online (Sandbox Code Playgroud)

JJS*_*JJS 28

有时您也想在创建对话框后动态添加按钮.在动态添加按钮到对话框的问题中 查看我的答案

var mydialog = ... result of jqueryui .dialog()
var buttons = mydialog.dialog("option", "buttons"); // getter
$.extend(buttons, { foo: function () { alert('foo'); } });
mydialog.dialog("option", "buttons", buttons); // setter
Run Code Online (Sandbox Code Playgroud)


Phi*_*ler 23

$('#msgBox').dialog({
    autoResize: true,
    show: "clip",
    hide: "clip",
    height: 'auto',
    width: 'auto',
    autoOpen: false,
    modal: true,
    position: 'center',
    draggable: false,

    open: function (type, data) {
        $(this).parent().appendTo("form");
    },

    buttons: { "OK": function() { $(this).dialog("close"); } } 
});
Run Code Online (Sandbox Code Playgroud)

  • 啊.我省略了一个逗号.我已经更新了这个例子. (2认同)

Dal*_*ale 12

如果要向已打开的对话框添加按钮,可以执行以下操作:

var buttonSet = $('#dialog').parent().find('.ui-dialog-buttonset');
var newButton = $('<button>My New Button</button>');
newButton.button().click(function () {
    alert('My new button clicked');
});
buttonSet.append(newButton);
Run Code Online (Sandbox Code Playgroud)


Tim*_*Tim 6

这是一个例子

将此添加到您的函数:

buttons: {
                OK: function() { //submit
                    $( this ).dialog( "close" );
                },
                Cancel: function() { //cancel
                    $( this ).dialog( "close" );
                }
            }
Run Code Online (Sandbox Code Playgroud)

所以你得到了

    $('#msgBox').dialog({


                autoResize: true,
                show: "clip",
                hide: "clip",
                height: 'auto',
                width: 'auto',
                autoOpen: false,
                modal: true,
                position: 'center',
                draggable: false,
                buttons: {
                OK: function() { //ok
                    $( this ).dialog( "close" );
                },
                Cancel: function() { //cancel
                    $( this ).dialog( "close" );
                }
            }
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }


            });
Run Code Online (Sandbox Code Playgroud)