简体   繁体   English

重新定义jQuery对话框按钮

[英]Redefining a jQuery dialog button

In our application we use a general function to create jQuery dialogs which contain module-specific content. 在我们的应用程序中,我们使用通用函数来创建包含特定于模块的内容的jQuery对话框。 The custom dialog consists of 3 buttons (Cancel, Save, Apply). 自定义对话框包含3个按钮(取消,保存,应用)。 Apply does the same as Save but also closes the dialog. “应用”与“保存”相同,但也会关闭对话框。

Many modules are still using a custom post instead of an ajax-post. 许多模块仍在使用自定义帖子而不是ajax-post。 For this reason I'm looking to overwrite/redefine the buttons which are on a specific dialog. 因此,我希望覆盖/重新定义特定对话框上的按钮。

So far I've got the buttons, but I'm unable to do something with them. 到目前为止,我已经有了按钮,但是我无法对它们做任何事情。 Is it possible to get the buttons from a dialog (yes, I know) but apply a different function to them? 是否可以从对话框中获取按钮(是的,我知道),但是可以对它们应用其他功能?

My code so far: 到目前为止,我的代码:

function OverrideDialogButtonCallbacks(sDialogInstance) {
  oButtons = $( '#dialog' ).dialog( 'option', 'buttons' );
  console.log(oButtons); // logs the buttons correctly

  if(sDialogInstance == 'TestInstance') {
    oButtons.Save = function() {
      alert('A new callback has been assigned.');
      // code for ajax-post will come here.
    }
  }
}
$('#dialog').dialog({
'buttons' : {
    'Save' : {
        id:"btn-save", // provide the id, if you want to apply a callback based on id selector
        click: function() {
           //
        },
    },
}
});

Did you try this? 你有尝试过吗? to override button's callback based on the need. 根据需要覆盖按钮的回调。

No need to re-assign at all. 完全不需要重新分配。 Try this. 尝试这个。

function OverrideDialogButtonCallbacks(dialogSelector) {
    var button = $(dialogSelector + " ~ .ui-dialog-buttonpane")
                    .find("button:contains('Save')");

    button.unbind("click").on("click", function() {
        alert("save overriden!");
    });
}

Call it like OverrideDialogButtonCallbacks("#dialog"); OverrideDialogButtonCallbacks("#dialog");一样调用它

Working fiddle: http://jsfiddle.net/codovations/yzfVT/ 工作提琴: http : //jsfiddle.net/codovations/yzfVT/

You can get the buttons using $(..).dialog('option', 'buttons') . 您可以使用$(..).dialog('option', 'buttons') This returns an array of objects that you can then rewire by searching through them and adjusting the click event: 这将返回一个对象数组,然后可以通过搜索对象并调整click事件来重新连接对象:

// Rewire the callback for the first button
var buttons = $('#dialog').dialog('option', 'buttons');
buttons[0].click = function() { alert('Click rewired!'); };

See this fiddle for an example: http://jsfiddle.net/z4TTH/2/ 有关示例,请参见此提琴: http : //jsfiddle.net/z4TTH/2/

If necessary, you can check the text of the button using button[i].text . 如有必要,可以使用button[i].text检查按钮的文本。


UPDATE: 更新:

The buttons option can be one of two forms, one is an array as described above, the other is an object where each property is the name of the button. buttons选项可以是两种形式之一,一种是如上所述的数组,另一种是对象,其中每个属性都是按钮的名称。 To rewire the click event in this instance it's necessary to update the buttons option in the dialog: 要在这种情况下重新连接click事件,必须更新对话框中的buttons选项:

// Rewire the callback for the OK button
var buttons = $('#dialog').dialog('option', 'buttons');
buttons.Ok = function() { alert('Click rewired!'); };

$('#dialog').dialog('option', 'buttons', buttons);

See this fiddle: http://jsfiddle.net/z4TTH/3/ 看到这个小提琴: http : //jsfiddle.net/z4TTH/3/

Can you try binding your new function code with Click event of Save? 您可以尝试将新功能代码与Save的Click事件绑定吗?

if(sDialogInstance == 'TestInstance') {
            $('#'+savebtn_id).click(function() {
              alert('A new callback has been assigned.');
              // code for ajax-post will come here.
            });
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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