1

ダイアログが開かれたときにトリガーされる ajax 呼び出しによって読み込まれるフォームを含む jQuery ダイアログを開いています。ユーザーが [OK] をクリックすると、ajax を使用してフォームを送信する前にフォームを検証します。OK ボタンに割り当てられたコールバックは次のとおりです。

var dialogOK = function () {
    var $form = $(this).find("form:first");
    if ($form.find("input:first").length > 0) {
        $form.removeData("validator").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse($form);
        $form.validate({
            submitHandler: function (form) {
                alert("This alert doesn't appear");
            },
            invalidHandler: function (event, validator) {
                alert("Neither does this one");
            }
        });
        if ($form.valid) {
            alert("form.valid says it's valid when it's not");
            //$form.submit(); //when this is called the client-side 
                             //validation works and the form is not submitted
            //but when I call ajaxSubmit (forms jQuery plugin)
            //the form is submitted
            $form.ajaxSubmit({
                success: function () {
                    alert("And I still need to work out how to check whether 
                        server-side validation passed before I close the dialog");
                }
            });
        }

    }
    else {
        $(this).dialog("close");
    }
};

この質問に基づいてコードを作成しましたが、ajax送信を行うときにクライアント側の検証を起動する方法がわかりません。

私はjquery-1.9.1、jquery.unobtrusive-ajax、jquery.validate、jquery.formを使用しています

4

1 に答える 1

2

プラグインメソッドの値が有効であることを確認する必要があるため、これが必要です

if ($form.valid()) 
{
  // do stuff
}

これではない

if ($form.valid) 
{
  // always true, as presence of the plugin method is 'truthy'
}
于 2013-06-07T13:21:31.847 に答える