1

ユーザーからの応答を確認するために .net プロジェクトで使用する jQuery でポップアップする Yes/No 確認ウィンドウを作成する関数を作成しました。クリックした内容に応じて機能をトリガーするボタンを生成しました。

//ローカライズされた方法で物事を確認するための特別なポップアップをロードする function popupConfirm(TitleKey, InstructionsKey, YesFunction, NoFunction) {

//create the service to give us the content
var agpc = new AjaxServices.AjaxPopUps();

//get the results and open the popup with the functions aligned
agpc.GetLocalizedStrings(new Array(TitleKey, InstructionsKey, "Yes", "No"), function (results) {
    var content = jQuery.parseJSON(results.toString());

    //get the buttons for confirm/or not
    var YesNoButtons = jQuery('<div></div>', { id: 'YesNoButtons' });
    var yesButton = jQuery('<a>' + content.Yes + '</a>');
    var noButton = jQuery('<a>' + content.No + '</a>');

    //add the event handlers
    yesButton.click(YesFunction);
    noButton.click(NoFunction);

    //set a nice pointer for mouse over
    yesButton.css({ cursor: 'pointer' });
    noButton.css({ cursor: 'pointer' });

    YesNoButtons.append(yesButton).append(noButton);
    //show the box
    openPopup("Non-JSON", YesNoButtons, eval('content.' + TitleKey), eval('content.' + InstructionsKey));

    });
}

さて、難しい部分が来ます。同社はまた、キーを押して yes/no 機能をトリガーすることも望んでいます。Enter キーは yes をトリガーし、エスケープは no をトリガーする必要があります。このタイプのセットアップでこれを行う方法がわかりません。

そこにあるほとんどのコードは無視できます。サーバーからローカライズされた文字列を取得することです。私が理解できないのは、keydown()イベントを追加することです。

何か案は?

4

1 に答える 1

1

これを行うのはそれほど難しいことではありません。次のクロスブラウザー対応コードをカスタム調整して使用してみてください。

イベント キー (キーボードで押されたキー) をキャッチし、いくつかのアクションを実行する必要がある場合:

$("#YourDialog").keypress( function(event) {  // when you do a key press inside of the element with ID YourDialog 
    var keyCode = event.keyCode ? event.keyCode : event.which;
    if( event.keyCode == 13 || event.keyCode == 27 ) { // catching event for clicking either enter key or escape key.
     $("#OkButton").trigger('click');
     // or $("#YourDialog").hide(); to hide your dialog.
    }
});

逆に、デフォルトのキー アクションを防止する必要がある場合は、次のコードを使用する必要があります。

$("#YourDialog").keypress( function(event) {  // when you do a key press inside of the element with ID YourDialog 
    var keyCode = event.keyCode ? event.keyCode : event.which;
    if( event.keyCode == 13 || event.keyCode == 27 ) { // catching event for clicking either enter key or escape key.
     event.preventDefault();  // preventing the default action - so, in this case, when the enter or escape is pressed nothing will happen. Especially it's important when you want to prevent user from click some keys. 
     event.stopPropagation();
    }
});

ユーザー イベント (押されたキー) をキャッチする必要があります。キー コード 13 はエンター キーを指し、キー コード 27 はエスケープ キーを指します。keyCode 変数は、Internet Explorer を含むすべてのブラウザーで普遍的に機能します。

完全なkeyCode リストについては、こちらも参照してください。

これがうまくいくことを願っています。

于 2012-08-22T18:06:32.927 に答える