11

I have a jQuery modal where I want to add an additional button if a conditional statement is met.

Original sample code (cutdown):

$("#dialog").html("<div></div>").dialog({
    title: "Some Title",
    modal: true,
    width: 550,
    buttons: {
        Ok: function() {
            //
        },
        'A Button': function() {
            //
        }
    }
}).dialog('open');

So as you see above there is a modal with 2 buttons, but I also want to add in there some dynamic code to be able to cater for an additional button if a variable is set. e.g.

var some_variable = 0;

$("#dialog").html("<div></div>").dialog({
    title: "Some Title",
    modal: true,
    width: 550,
    buttons: {
        Ok: function() {
            //
        },
        'A Button': function() {
            //
        }
        /* ??? */
        if (some_variable==1) //then add the other button's code here..
        /* ??? */
    }
}).dialog('open');
4

3 に答える 3

20

buttonsダイアログを作成する前に、オブジェクトを作成できます。

//Create the buttons object
var buttons = {
    Ok: function() {},
    'A Button': function() {}
};

//Add another button to that object if some condition is true
if(something) {
    buttons['B button'] = function() {};
}

//Create the dialog, passing in the existing buttons object
$("#dialog").html("<div></div>").dialog({
    buttons: buttons,
    //Other options
});
于 2012-05-28T12:51:15.280 に答える
3

現在、ボタンを 1 つだけ変更する方法はありませんが、オプション メソッドを使用してボタン ハッシュ全体をリセットできます。

条件が満たされている場合は、ボタンをもう一度リセットしてください。

$('#contactform').dialog('option', 'buttons', {...});

http://old.nabble.com/jQuery-dialog-add-remove-button-on-the-fly-td22036498s27240.html

于 2012-05-28T12:52:23.437 に答える