需要单击两次以进行jquery提交

时间:2012-12-01 07:28:55

标签: php jquery ajax

$(document).ready(function () {
$.validator.setDefaults({
    submitHandler: function () {
        $('#search').submit(function () {
            var city = $("#city").val();
            var adults = $("#adults").val();
            var children = $("#children").val();
            var SDate = $("#hotelStartDate").val();
            var EDate = $("#hotelEndDate").val();
            SDate = SDate.substr(6, 4) + '-' + SDate.substr(3, 2) + '-' + SDate.substr(0, 2);
            EDate = EDate.substr(6, 4) + '-' + EDate.substr(3, 2) + '-' + EDate.substr(0, 2);
            jQuery(":submit", this).css("display", "none");
            jQuery(":submit", this).after("<span class='button nice awesome green large radius'>Searching...</span>");
            location.href = 'http://www.dhitrax.com/destination.php?checkin=' + SDate + '&checkout=' + EDate + '&city=' + city + '&adults=' + adults + '&children=' + children;
            return false;
        });
    },
    highlight: function (input) {
        $(input).addClass("ui-state-highlight");
    },
    unhighlight: function (input) {
        $(input).removeClass("ui-state-highlight");
    }
});
$().ready(function () {
    $.fn.themeswitcher && $('<div/>').css({
        position: "absolute",
        right: 10,
        top: 10
    }).appendTo(document.body).themeswitcher();
    $("#search").validate({
        rules: {
            city: "required",
            hotelStartDate: "required",
            hotelEndDate: "required"
        },
        messages: {
            City: "Please type your destination",
            hotelStartDate: "Select check in Date",
            hotelEndDate: "Select check out Date"
        }
    });
});
});

1 个答案:

答案 0 :(得分:0)

您不应将事件处理程序绑定到现有submit jQuery validate提供的} 内的submitHandler事件。它需要两次点击才能提交​​表单,因为第一次提交时,处理程序已绑定,第二次提交处理程序时执行。

这应该就像将代码移到submitHandler

一样简单
$.validator.setDefaults({
    submitHandler: function () {
        var city = $("#city").val();
        var adults = $("#adults").val();
        var children = $("#children").val();
        var SDate = $("#hotelStartDate").val();
        var EDate = $("#hotelEndDate").val();
        SDate = SDate.substr(6, 4) + '-' + SDate.substr(3, 2) + '-' + SDate.substr(0, 2);
        EDate = EDate.substr(6, 4) + '-' + EDate.substr(3, 2) + '-' + EDate.substr(0, 2);
        jQuery(":submit", this).css("display", "none");
        jQuery(":submit", this).after("<span class='button nice awesome green large radius'>Searching...</span>");
        location.href = 'http://www.dhitrax.com/destination.php?checkin=' + SDate + '&checkout=' + EDate + '&city=' + city + '&adults=' + adults + '&children=' + children;
    },
    highlight: function (input) {
        $(input).addClass("ui-state-highlight");
    },
    unhighlight: function (input) {
        $(input).removeClass("ui-state-highlight");
    }
});