在submitHandler中没有遇到Ajax函数

时间:2015-05-29 15:12:06

标签: javascript jquery ajax

表单验证有效,但我无法正确启动Ajax调用。正在到达submitHandler,但Ajax调用没有。我在底部包含了一个小提琴,但显然你不能从那里发出ajax调用。

    $(".player-code, .submit").hide();

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

                },
                failure: function () {
                    $('.render-info').html("<div class='alert alert-failure'>Submission Error</div>");
                }
            });
        }
    });
});

jQuery.validator.addMethod("domainChk", function (value, element, params) {
    if (this.optional(element)) return true;
    var regExp = new RegExp("^(?!www\\.|http:\/\/www\.)(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");
    return regExp.test(value);
}, "Valid hostname required for player code");

jQuery.validator.addClassRules({
    domainChk: {
        domainChk: true
    }
});

$('input[type="text"]').on('click keyup blur', function () {
    if ($('#form').valid()) {
        $(".submit").show();
    } else {
        $(".submit").hide();
    }
});

//PREPOPULATE ACCOUNT FROM QUERY STRING
var url = window.location.href; 
var regex = /=.*/; // match '=' and capture everything that follows
var accountId = url.match(regex);
    $(".accountNumber").val(accountId).remove("=");
//

jsFiddle:Link

2 个答案:

答案 0 :(得分:1)

failure:没有$.ajax()选项。如果您想查看ajax调用中发生的任何错误,请使用error:来捕获错误。

答案 1 :(得分:0)

要提交表单,您应该使用

<button class="btn btn-default submit" type="submit">Submit</button>

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

只有在原生表单提交时才会调用

submitHandler

Fiddle