0

クライアント側の目立たない検証を使用して、Visual Studio 10 で MVC 3 とカミソリを使用しています。必須フィールドの一部が欠落している場合に、フォームを保存する機会をユーザーに提供する必要があります。送信は Ajax.bgein フォーム構造です。

submit イベントをキャッチするための次の JavaScript があります。

$(document).ready(function () {

$('#submit-11').click(function (event) {

    var validator = $("#form0").validate();

    if (!$("#form0").valid()) {
        // for partial save needs either ID number
        // or family name and date of birth
        var NameExists = true;
        var DateOfBirthExists = true;
        var IDNumberExists = true;

        if (validator.errorMap["model.FamilyName"]) { NameExists = false; }
        if (validator.errorMap["model.DateOfBirth"]) { DateOfBirthExists = false; }
        if (validator.errorMap["model.IDNumber"]) { IDNumberExists = false; }

        if (IDNumberExists || (NameExists && DateOfBirthExists)) {
            if ($("#model_PartialSave").attr('checked')) {
                //  partial save has been requested
                alert("saving");
                return true;    // AJAX save is NOT triggered
            }
            $("#AgreePartialSave").show();
            $("#model_PartialSave").removeAttr('checked');

        }
        return false;
    }
    return true;    // AJAX save IS triggered
});

});                    //document ready end

ロジックは正しいようです-私はこれをfirebugで追跡しました-アラートはトリガーされますが、AJAX送信呼び出しは発生しません。フォームが有効な場合、AJAX 送信呼び出しが発生します。

私も試しevent.preventDefault(); ましたが、それは保存の発生を防ぎます。

私もこれをfirebugで追跡しましたが、すべての正しい呼び出しが行われているようです。

感謝して受け取った助け。

アップデート

これは Microsoft Ajax ライブラリの制限のようです。詳細と回避策については、このリンクを参照してください。

4

1 に答える 1

0

I believe what you need to intercept is the form's submit event, not the submit button's click event. What's happening (I suspect) is that, since the validation framework still considers your form to be invalid, it's not submitting it, even though you're letting the click event go through with your return true.

There's an example here of running javascript after validation has completed but before the actual post.

$(function() { 
    $('#form0').submit(function() { 
        if (!$(this).valid()) { 
            // do your thing
            return true; 
        } 
    }); 
 }); 

ETA: having read your reply, I wonder whether the problem isn't that the validation framework considers your page invalid, and you're attempting to override its conclusion and submit anyway. In that case, the code I provided would never execute because the submission would never happen.

I would suggest trying this: remove the Required attributes from the properties in the model that correspond to the 3 input fields, and move your checking logic into the submit function, as I suggested. But rather than saying, if(!$(this).valid()), you would only execute the code if the form was otherwise valid.

于 2012-10-15T17:39:15.753 に答える