0

リンクのクリック機能を使用して ajax 経由でフォームを送信しています。サーバーがエラーを返すと、サーバー側のエラーが表示されます。

この方法だとフォーム内でエンターキーが効かなくなりますが、おそらくjqueryバリデーションのsubmitハンドラを使ったほうが簡単で正しい方法があると思います。

これまでの私のコードは次のとおりです...

function loginSubmit(){
    $.ajax({
        url: loginFormURL,          
        type: 'POST',
        /** contentType: "application/json;",  **/
        dataType:"text json",
        /** async: true, **/
        data:$('#loginForm').serialize(),
        success: function(data){

           if(data.isError == "false" || data.isError == ""){

                       $.postMessage(
                        'redirectURL|'+ redirectURL +'',
                        '*',
                        parent
                      );


                     } else

                     if(data.isError == "true"){

                       $("#loginError").empty().show(); 
                       // iterate over the message list and display the error
                       for (var i=0; i < data.errorMessages.length; i++){
                               // inject error message to the error container

                           $("#loginError").append(data.errorMessages[i]);

                           }    

                       // iterate over the field list and paint the fields in red

                       $('input').parent('div').addClass('inputError');



                   }

           }            
   });
} 

そしてクリック機能:

 $("a#loginButton").click(function(){
                $("#loginForm").validate().form(this);
                if($('#loginForm').valid()){
                    loginSubmit();
                }
            });

どんな助けでも大歓迎です。

4

1 に答える 1

1

入力フィールドでの ENTER キーの押下をリッスンする必要があります。

$('input').keypress(function(event) {
    if(event.which == 13) {
        loginSubmit();
        event.preventDefault();
        return false;
    }
});

もちろん、すべての INPUT タグで ENTER をキャプチャしたくない場合があるため、必要に応じて別のセレクタを使用できます。

ここで同様の議論を参照してください。

于 2012-07-13T01:37:36.867 に答える