0

jQuery ajax 调用后如何恢复/停止表单提交?我有 MVC 应用程序,我正在调用一个操作json,如果结果为假,我想停止表单提交,如果结果为真,我想恢复。

查询:

   $("#formElem").submit(function (e) {
        e.preventDefault();
        $.ajax({
            url: '@Url.Action("Check", "TimeRanges")',
            type: "GET",
            data: {startRange: $('#SelectedStartTimeRange').val() , endRange: $('#SelectedEndTimeRange').val()},
            aync: false,
            dataType: 'json',
            success: function (data) {
                if(data == false) {
                    $("#rangeexist").html('Error');
                    return false;
                } else {
                    return true;
                }
            }
        });
    });

行动

public JsonResult Check(string startRange, string endRange)
{
    var result = false;
    if (!String.IsNullOrEmpty(startRange) && !String.IsNullOrEmpty(endRange))
    {
        TimeSpan spanStart;
        TimeSpan.TryParse(startRange, out spanStart);

        TimeSpan spanEnd;
        TimeSpan.TryParse(endRange, out spanEnd);

        var timeRangExisted = _repo.All().Where(x => x.TimeFrom.Equals(spanStart) && x.TimeTo.Equals(spanEnd)).ToList();
        if (!timeRangExisted.Any())
            result = true;
    }

    return Json(result, JsonRequestBehavior.AllowGet);
}
4

1 回答 1

0
var ajaxSent = false;
$("#formElem").submit(function (e) {
    if ( !ajaxSent )
     e.preventDefault();
    $.ajax({
        url: '@Url.Action("Check", "TimeRanges")',
        type: "GET",
        data: {startRange: $('#SelectedStartTimeRange').val() , endRange: $('#SelectedEndTimeRange').val()},
        aync: false,
        dataType: 'json',
        success: function (data) {
            if(data == false) {
                $("#rangeexist").html('Error');
                ajaxSent = true;
                $("#formElem").submit(); // something like that ....
                return false;
            } else {
                return true;
            }
        }
    });
});
于 2013-03-03T12:05:11.710 回答