在jquery对话框函数中动态调用javascript函数

时间:2020-12-28 01:24:49

I want to call a function as parameter in jQuery dialog when the yes button is pressed. I am using the following code but this is not working.

我想在按下yes按钮时将函数作为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>

When I click on the button named "alert" this the function showYesNoAlertMsg is called and it shows the dialog box of id "boxDivId" perfectly but I want to call a function named "newFunc" on the yes button. I passed this function as a parameter but it is not working in the dialog property. If I uncomment the first commented line in showYesNoAlertMsg, This line works fine and calls the function "newFunc" perfectly. but the same line is not working in Yes button. Please tell me.

当我点击名为“alert”的按钮时,调用showYesNoAlertMsg函数,它显示id“boxDivId”的对话框,但我想在yes按钮上调用名为“newFunc”的函数。我将此函数作为参数传递,但它在对话框属性中不起作用。如果我取消注释showYesNoAlertMsg中的第一个注释行,这行正常工作并完美地调用函数“newFunc”。但是同一行在“是”按钮中不起作用。请告诉我。

Thanks

1 个解决方案

#1


1  

In similar situation I had used such approach:

在类似的情况下我使用了这种方法:

 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']);" />

If you want to use arguments, you need to cache it value in showYesNoAlertMsg context into some variable as in Yes button's click event handler it's already an arguments of this handler function

如果你想使用参数,你需要将它在showYesNoAlertMsg上下文中的值缓存到某个变量中,就像在Yes按钮的click事件处理程序中一样,它已经是这个处理函数的参数

#1


1  

In similar situation I had used such approach:

在类似的情况下我使用了这种方法:

 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']);" />

If you want to use arguments, you need to cache it value in showYesNoAlertMsg context into some variable as in Yes button's click event handler it's already an arguments of this handler function

如果你想使用参数,你需要将它在showYesNoAlertMsg上下文中的值缓存到某个变量中,就像在Yes按钮的click事件处理程序中一样,它已经是这个处理函数的参数