开发者

Setting a jQuery dialog button callback

开发者 https://www.devze.com 2023-02-07 16:35 出处:网络
Hey coders, i would like to initialize a dialog box with a callback function for say a \'save\' button but i want the callback to reside as a standalone function rather than defined inline using funct

Hey coders, i would like to initialize a dialog box with a callback function for say a 'save' button but i want the callback to reside as a standalone function rather than defined inline using function(){....} the code snippet below highlights what I want to do.

$( "#dialog开发者_C百科-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Save": saveAction() 
...
function saveAction()  
{  
}  

what is the proper syntax for the "Save": saveAction() line cause it is doesn't seem to work?

thanks


The parens after saveAction makes the function execute. Use this instead:

        "Save": saveAction


saveAction must have parameters defined in the signature: i.e. saveAction(a,b,c), then when setting the callback do this:

"Save": saveAction({a = "val", b = "val", c = "val"})


If you have to pass in parameters, you must wrap your function call in an anonymous function definition, like this:

"Save": function() { saveAction({a = "val", b = "val", c = "val"}) }

This effectively defines a new anonymous function that takes no parameters, and which when executed will call your own function with your desired paramters.

0

精彩评论

暂无评论...
验证码 换一张
取 消