0

そのため、AJAX を介して送信し、成功したかどうかにかかわらずダイアログを表示する機能があります。すべて正常に動作しますが、そのダイアログ関数に追加機能を実行する追加機能 (オプション) を渡すオプションが必要です。ここに私が持っているものがあります:

// the work
if (data._response.success == 'true') {
  $("#suppliers_grid").trigger("reloadGrid");
  $('#manage-suppliers-form').fullFormReset();
  alertDialog('Supplier '+ action +' successfully!','Success',$('#manage-suppliers-form :input:visible:first').focus());
} else {
  alertDialog('Supplier was not '+ action +' successfully!<br />Please try again or report this to the administrator.','Error','ui-state-error');
}

// the alertDialog function
function alertDialog(message,title,cssClass,closeFunction) {
  title = typeof title !== 'undefined' ? title : 'Notice';
  cssClass = typeof cssClass !== 'undefined' ? cssClass : 'ui-state-highlight';
  if (cssClass=='ui-state-error') {
    icon = 'ui-icon-alert';
  }
  else {
    icon = 'ui-icon-info';
  }

  var dialog = $('<div><p><span class="ui-icon '+ icon +'"></span>'+ message +'</p></div>');
  dialog.dialog({
      modal: true,
      title: title,
      buttons: {
        Ok: function() { $(this).dialog("close");   }
      },
      close: closeFunction()
  });   
}

1) closeFunction を渡すと、上記は機能しません

2)何かを渡さずにテストしていませんが、どちらも機能しないと確信しています。closeFunction は OPTIONAL にする必要があります

3) alertDialog 呼び出しの後にコードの「フォーカス」行を単純に配置することはできません。これは機能しますが(バックグラウンドでフォーカスを取得します)。誰かが alertDialog で「OK」をクリックするとすぐに、フォーカスが失われるため、alertDialog の終了時に呼び出す必要があります。

4

2 に答える 2

1

まず第一に、あなたが質問で言及したものは何でも(関数に関数を渡す) 、Javascriptプログラミングの世界では「コールバック」関数と呼ばれます。

ところで、あなたの質問に答えるには、次のようにダイアログを閉じるときに呼び出されるクローズ イベント コールバック関数をバインドする必要があると思います。

    var that = this;
    dialog.dialog({
        close: function( event, ui ) {
            if(closeFunction && typeof closeFunction === 'function') {
                closeFunction.call(that);
            }
    }});

closeFunction 内で、必要な要素に対して focus() を実行してみてください。

これを読んでみてください!close イベントのコールバックの使用法について理解を深めるために。

それでも私の答えから適切な解決策が得られない場合は、フィドルを投稿するか、直面している問題をよりよく理解してください!

于 2013-05-30T10:15:57.487 に答える
0

試す

// the work
if (data._response.success == 'true') {
    $("#suppliers_grid").trigger("reloadGrid");
    $('#manage-suppliers-form').fullFormReset();
    //Pass the function as the fourth parameter and create a annonymous function to set the focus
    alertDialog('Supplier '+ action +' successfully!', 'Success', '', function(){
        $('#manage-suppliers-form :input:visible:first').focus();
    });
} else {
    alertDialog('Supplier was not '+ action +' successfully!<br />Please try again or report this to the administrator.','Error','ui-state-error');
}

// the alertDialog function
function alertDialog(message,title,cssClass,closeFunction) {
    title = typeof title !== 'undefined' ? title : 'Notice';
    cssClass = typeof cssClass !== 'undefined' ? cssClass : 'ui-state-highlight';
    if (cssClass=='ui-state-error') {
        icon = 'ui-icon-alert';
    }
    else {
        icon = 'ui-icon-info';
    }

    var dialog = $('<div><p><span class="ui-icon '+ icon +'"></span>'+ message +'</p></div>');
    dialog.dialog({
        modal: true,
        title: title,
        buttons: {
            Ok: function() { $(this).dialog("close");   }
        },
        close: closeFunction //assign the function reference instead of the return value of the function
    });   
}
于 2013-06-19T07:20:02.377 に答える