[英]Call a javascript function dynamically in jquery dialog function
当要按“是”按钮时,我想在jQuery对话框中调用函数作为参数。 我正在使用以下代码,但这不起作用。
function showYesNoAlertMsg(divId,optFunction){
//window[optFunction].apply(null, Array.prototype.slice.call(arguments, 2));
$('#'+divId).dialog('open');
$('#'+divId).dialog({
autoOpen: true,
width: 400,
height: 175,
modal: true,
resizable: false,
buttons: {
"Yes": function() {
window[optFunction].apply(null,
Array.prototype.slice.call(arguments, 2));
$(this).dialog("close");
},
"No": function() {
$(this).dialog("close");
}
}
});
}
function newfunc(a,b){
alert(a+'--'+b);
}
<input type="button" name="alert" id="alert"
onclick="showYesNoAlertMsg('boxDivId','newfunc','aaaaa','bbbbb');"/>
<div id="boxDivId">
hello
</div>
当我单击名为“ alert”的按钮时,将调用函数showYesNoAlertMsg
,它会完美显示id为“ boxDivId”的对话框,但我想在yes按钮上调用名为“ newFunc”的函数。 我将此函数作为参数传递,但在dialog属性中不起作用。 如果我取消注释showYesNoAlertMsg
的第一条注释行,则该行可以正常工作,并且可以完美地调用函数“ newFunc”。 但是同一行在“是”按钮中不起作用。 请告诉我。
谢谢
在类似情况下,我使用了这种方法:
function showYesNoAlertMsg(divId, optFunction, optFunctionParams) {
if (!$.isArray(optFunctionParams)) {
optFunctionParams = Array.prototype.slice.call(arguments, 2);
}
$('#' + divId).dialog({
autoOpen: true,
width: 400,
height: 175,
modal: true,
resizable: false,
buttons: {
"Yes": function () {
if (optFunction && typeof optFunction == "function") {
optFunction.apply(window, optFunctionParams || []);
}
$(this).dialog("close");
},
"No": function () {
$(this).dialog("close");
}
}
});
}
function newfunc(a, b) {
alert(a + '--' + b);
}
<input type="button" name="alert" id="alert" value="Click Me"
onclick="showYesNoAlertMsg('boxDivId', newfunc, ['aaaaa','bbbbb']);" />
如果要使用arguments
,则需要将其值在showYesNoAlertMsg
上下文中缓存到某个变量中,如“ Yes
按钮的click事件处理程序中一样,它已经是该处理程序函数的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.