验证不会触发submitHandler

时间:2015-05-28 18:09:23

标签: javascript jquery jquery-validate

如标题中所述,submitHandler未触发/到达,但验证工作正常。提交处理程序是正确的,但没有触发。

$(function () {
$("#form").validate({
    rules: {
        domain: {
            required: true
        },
        playerClass: {
            required: true,
        }
    },
    submitHandler: function (form) {
        var accountNumber = $("input#accountNumber").val();
        var domain = $("input#domain").val();
        var playerClass = $("input#playerClass").val();
        var dataString = JSON.stringify([{
            "accountNumber": accountNumber,
                "playerClass": playerClass
        }]);
        //Save Form Data........
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/",
            contentType: "application/json",
            data: dataString,
            success: function () {
                $('.render-info').html("<div class='alert alert-success'>You've successfully built your player code</div>");
                $(".player-code").show();
            },
            failure: function () {
                alert(dataString);
                $('.render-info').html("<div class='alert alert-failure'>Submission Error</div>");
            }
        });
        $(".player-code").show();
    }
});
});

jsFiddle:Link

1 个答案:

答案 0 :(得分:0)

因为您没有真正的提交按钮。 div不是按钮......

<div class="btn btn-default submit">Submit</div>

当表单有效时,您需要buttoninput type="submit"来触发submitHandler

<input type="submit" class="submit" />

OR

<button type="submit" class="submit">Submit</button>

http://jsfiddle.net/e04rca0t/7/