jQuery when().done() don't work for me

2019-08-06 03:03发布

问题:

I have the jQuery code below, but the when().done() does not work as expected for me. The updateResultFooter() is called bedore the doReverseSearch() method finish her its work. and as a result of that, a button in my view is enabled, and then re-take his default value (desabled) after the replace in the doReverseSearch() method.

$("#idBnSearch").click(function () 
{
    $.when(doReverseSearch(telValue, pageIndex, methodUrl))
    .done(function () 
    {
        updateResultFooter("@ViewBag.CountResult", pageIndex, "@ViewBag.PageCount");
    });

});

function updateResultFooter(resultCount, pageIndex, pageCount) 
{
    if (pageIndex == 0)
        $("#bnPreviousPage").attr('disabled', 'disabled');
    else
        $("#bnPreviousPage").removeAttr('disabled');

    if ((pageIndex + 1) == pageCount)
        $("#bnNextPage").attr('disabled', 'disabled');
    else
        $("#bnNextPage").removeAttr('disabled');
    }

function doReverseSearch(telValue, pageIdx, methodUrl) 
{
    $.ajax(
    {
        url: methodUrl,
        type: 'post',
        data: JSON.stringify({ Telephone: telValue, pageIndex: pageIdx }),
        datatype: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $('#result').replaceWith(data);
        },
        error: function (request, status, err) {
            alert(status);
            alert(err);
        }
    });
}

Thank you in advance