如何在创建Jquery对话框UI后添加按钮

如何在创建Jquery对话框UI后添加按钮,jquery,jquery-ui,Jquery,Jquery Ui,我想创建一个jquery对话框,需要向其中添加按钮 我使用的代码在IE中运行良好,但在Mozilla中不起作用 有人能弄清楚这里的问题是什么吗 function dialog_box(dynDiv, rootTemplate) { var dialog_buttons = rootTemplate.buttons; var dialog = $("#" + dynDiv.id).dialog({ hide: "explode", title: ro

我想创建一个jquery对话框,需要向其中添加按钮

我使用的代码在IE中运行良好,但在Mozilla中不起作用

有人能弄清楚这里的问题是什么吗

function dialog_box(dynDiv, rootTemplate) {
    var dialog_buttons = rootTemplate.buttons;
    var dialog = $("#" + dynDiv.id).dialog({
        hide: "explode",
        title: rootTemplate.etype,
        buttons:'',
        text: rootTemplate.text,
        resizable: true,
        minWidth: 200,
        minHeight: 150,
        close: function () {
            $(dialog).dialog('destroy').remove();
        }
    });


    var buttonSet = $("#" + dynDiv.id).parent().find('.ui-dialog-buttonset');
    $.each(dialog_buttons, function (index, props) {
        var newButton = $('<button></button>', {
            id: "btn" + dynDiv.id + props.id,
            text: props.text
        });
        newButton.button().unbind().on("click", props.handler);

        $(buttonSet).append(newButton);      
    });


}
函数对话框(dynDiv,rootTemplate){
var dialog_buttons=rootTemplate.buttons;
变量对话框=$(“#”+dynDiv.id)。对话框({
隐藏:“爆炸”,
标题:rootTemplate.etype,
按钮:“”,
text:rootTemplate.text,
可调整大小:正确,
最小宽度:200,
身高:150,
关闭:函数(){
$(dialog.dialog('destroy').remove();
}
});
var buttonSet=$(“#”+dynDiv.id).parent().find('.ui对话框buttonSet');
$。每个(对话框按钮、功能(索引、道具){
var newButton=$(''{
id:“btn”+动态id+道具id,
文本:props.text
});
newButton.button().unbind().on(“单击”,props.handler);
$(按钮集).append(新按钮);
});
}

我认为在创建对话框时,可以使用按钮选项将按钮创建为选项:

试试这个:

// Retrieve buttons hash
var buttons = dialog.dialog("option", "buttons"); 
// Extend the hash (so you're able to keep the old buttons)
$.extend(buttons, { props.text: props.handler } );
// Set it again
dialog.dialog("option", "buttons", buttons);

我对发布的代码做了一些小改动,效果很好。已修改部分高亮显示

function dialog_box(dynDiv, rootTemplate) {
        var dialog_buttons = rootTemplate.buttons;
        var dialog = $("#" + dynDiv.id).dialog({
            hide: "explode",
            title: rootTemplate.etype,
            **buttons:[{}],**
            text: rootTemplate.text,
            resizable: true,
            minWidth: 200,
            minHeight: 150,
            close: function () {
                $(dialog).dialog('destroy').remove();
            }
        });


        var buttonSet = $("#" + dynDiv.id).parent().find('.ui-dialog-buttonset');
        **$(buttonSet).empty();**
        $.each(dialog_buttons, function (index, props) {
            var newButton = $('<button></button>', {
                id: "btn" + dynDiv.id + props.id,
                text: props.text
            });
            newButton.button().unbind().on("click", props.handler);

            $(buttonSet).append(newButton);      
        });


    }
函数对话框(dynDiv,rootTemplate){
var dialog_buttons=rootTemplate.buttons;
变量对话框=$(“#”+dynDiv.id)。对话框({
隐藏:“爆炸”,
标题:rootTemplate.etype,
**按钮:[{}]**
text:rootTemplate.text,
可调整大小:正确,
最小宽度:200,
身高:150,
关闭:函数(){
$(dialog.dialog('destroy').remove();
}
});
var buttonSet=$(“#”+dynDiv.id).parent().find('.ui对话框buttonSet');
**$(buttonSet).empty()**
$。每个(对话框按钮、功能(索引、道具){
var newButton=$(''{
id:“btn”+动态id+道具id,
文本:props.text
});
newButton.button().unbind().on(“单击”,props.handler);
$(按钮集).append(新按钮);
});
}
因此,在创建JqueryUI对话框时添加了一个空按钮集。
清除按钮集后,我已将按钮添加到其中。

我无法指定您提供的值。我需要从输入变量中获取值来创建按钮。您可以在创建对话框后设置该选项,也可以调用$(“.selector”)。对话框(“选项”、“按钮”和…)我修改了示例代码。尽管存在语法错误,但它是否有效?要在@KT上展开,还可以添加其他按钮,如
$.extend({},{props.text:props.handler},buttons)
您是否尝试过@KT利用
$.extend
?如果您在指定的初始化按钮之前需要新按钮,还有一种替代方法:
$.extend({},{props.text:props.handler},buttons)
function dialog_box(dynDiv, rootTemplate) {
        var dialog_buttons = rootTemplate.buttons;
        var dialog = $("#" + dynDiv.id).dialog({
            hide: "explode",
            title: rootTemplate.etype,
            **buttons:[{}],**
            text: rootTemplate.text,
            resizable: true,
            minWidth: 200,
            minHeight: 150,
            close: function () {
                $(dialog).dialog('destroy').remove();
            }
        });


        var buttonSet = $("#" + dynDiv.id).parent().find('.ui-dialog-buttonset');
        **$(buttonSet).empty();**
        $.each(dialog_buttons, function (index, props) {
            var newButton = $('<button></button>', {
                id: "btn" + dynDiv.id + props.id,
                text: props.text
            });
            newButton.button().unbind().on("click", props.handler);

            $(buttonSet).append(newButton);      
        });


    }