jquery - jQuery 对话框按钮中的动态函数

标签 jquery modal-dialog jquery-ui-dialog

我遇到以下问题。我正在尝试在模式对话框中添加动态按钮。但我不知道如何为每个按钮赋予该功能。

我有以下内容:

     for(i=0;i<buttons.length;i++){
          arrButton[i] = { id :  buttons[i].name  , text :  buttons[i].label , click : function(){  buttons[i].onclick  } };
     } 
     $("#divFormulario").dialog
        ({  modal    : true, 
            title    : titulo,
            resizable: false,
            buttons  :  arrButton     
        });

例如,如果我有以下内容:buttons[i].onclick = "functionAlert();,当我创建按钮时,将包含带有 buttons[i] 的点击事件。 onclick,但我需要带有 functionAlert() 的 click 事件。我做错了什么?

最佳答案

假设你有一个函数:

function functionAlert() { ...some code... }

不要将函数作为字符串传递,只需执行以下操作:

buttons[i].onclick = functionAlert;

那么你的循环应该是:

for(i=0;i<buttons.length;i++){
   arrButton[i] = { id :  buttons[i].name  , text :  buttons[i].label , click : buttons[i].onclick };
}

关于jquery - jQuery 对话框按钮中的动态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5718082/

相关文章:

javascript - 鼠标移开时关闭引导模式对话框

jquery - 打开对话框时 IE 的奇怪行为

jquery - 有没有办法在 Twitter Bootstrap 模式对话框关闭后将文档放入 "focus"中?

javascript - JQuery 1.11.2 不能在初始化之前调用对话框的方法;试图调用方法 'destroy'

javascript - jQuery 可拖动位置不变

JQuery .load 弄乱了 HTML 文本样式

php - 将 WHERE 函数与 $_POST 一起使用

带有旋转插件的 jQuery flot 隐藏 x 轴标签

javascript - 视频-js : vjs-big-play-button isn't hidden after click

html - 防止 Bootstrap Modal 在单击外部或按 Escape 时消失?