简体   繁体   中英

jQuery dialog button how to set the click event?

Ok i got this code:

    $(document).ready(
    function() {
        $(".dialogDiv").dialog({
            autoOpen: false,
            modal: true,
            position: [50, 50],
            buttons: {
                "Print page": function() {
                    alert("Print");
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        }
        );
    $('.ui-dialog-buttonpane button:contains("Print page")').attr("id", "dialog_print-button");
    $(".dialogDiv").parent().appendTo($('form'));
    }

How do I assign or set a new function to the click event?

$("#dialog_print-button"). ???

Edit, This works:

$("#dialog_print-button").unbind("click").click(
function () {
   alert("new function that overide the old ones")
}
)

Tried to find how to do in the jQuery documentation but I think it's hard to find around in the documentation. Especially when new to javaScript and the jQuery libary.

Edit, A fast way to get help is to go to jQuery irc channel :D

I think this would help:

$(".dialogDiv").dialog("option", "buttons", {
    "Print page": function() { /* new action */ },
    "Cancel": function() { $(this).dialog("close"); }
});

Because buttons property sets all the buttons, you have to include cancel button handler.

$("#Print page").click(function () {
   ...
});

Or maybe it should be

$("#dialog_print-button").click(function () {
   ...
});

jQuery UI dialog buttons now supports the "id" attribute natively.

    $("#dialog-form").dialog({
        autoOpen: false,
        height: "auto",
        width: 300,
        buttons:
        [
            {
                text: "Create Revision",
                id: "btnCreateRev",
                click: function () {
                    //code for creating a revision
                }
            },
            {
                text: "Cancel",
                id: "btnCancel",
                click: function () { $(this).dialog("close"); },
            }
        ]
    });

You put the code within the button section:

 ...
 buttons: {                   
         "Print page": function() {                       
          //here you execute the code or call external functions as needed 
          }

Once you click the button on the Dialog, that code is automatically invoked. Therefore you insert there directly the code that implements your logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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