Dynamic functions in jQuery dialog buttons
I have the following problem. I'm trying to add dynamic buttons in a modal dialog. But I don´t know, how I can give the function to each button.
I have the following:开发者_开发百科
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
});
For example, if I have the following : buttons[i].onclick = "functionAlert();
, when I create the button have the click event with buttons[i].onclick
, but I need the click event with functionAlert()
. What am I doing wrong?
Assuming you have an function:
function functionAlert() { ...some code... }
Instead of passing functions as strings, just do:
buttons[i].onclick = functionAlert;
Then your loop should be:
for(i=0;i<buttons.length;i++){
arrButton[i] = { id : buttons[i].name , text : buttons[i].label , click : buttons[i].onclick };
}
精彩评论