2

はいボタンが押されたときに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」という名前の関数を呼び出したいと思います。この関数をパラメーターとして渡しましたが、ダイアログプロパティで機能していません。の最初のコメント行のコメントをshowYesNoAlertMsg外すと、この行は正常に機能し、関数「newFunc」を完全に呼び出します。しかし、同じ行が[はい]ボタンで機能していません。教えてください。

ありがとう

4

1 に答える 1

1

同様の状況で、私はそのようなアプローチを使用しました:

 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値をいくつかの変数にキャッシュする必要があります。これは、ボタンのクリック イベント ハンドラーのように、既にこのハンドラー関数の引数になっているためです。showYesNoAlertMsgYes

于 2012-10-22T10:09:25.197 に答える