2

I am using the DataTables plugin and the colorbox plugin. I am trying to have my function return true if the .post is a success.

jQuery('.msg_delete').live('click', function () {
        var aData = oTable.fnGetData(nTr);
        if (deleteMessage(aData[6]) == true) {
            jQuery.colorbox.close();
            oTable.fnDeleteRow(nTr);
        }
        return false;
    });

function deleteMessage(messageID) {
    jQuery.post('ajax/msg_functions.asp', { action: 'delete', messageid: messageID }, function (data) {

    })
    .success(function () { return true; })
    .error(function () { alert("There was an error while trying to make this request;  If it persists please contact support"); })
    ;
}

Right now it correctly does the post. I know this because the message is deleted. So I assume it goes to the .success and I would think would now return true. But it doesn't seem to be doing that.

It never seems to go to the colorbox.close() or fnDeleteRow. Can anyone see what I am missing here?

4

1 回答 1

4

您的 return 仅从当前函数返回,即您传入的匿名函数$.ajax

您的代码将无法按照当前的组织方式运行,使用示例。

jQuery('.msg_delete').live('click', function() {
    var aData = oTable.fnGetData(nTr);
    deleteMessage(aData[6]).done(function(){
        jQuery.colorbox.close();
        oTable.fnDeleteRow(nTr);
    });
    return false;
});

function deleteMessage(messageID) {
    return jQuery.post('ajax/msg_functions.asp', {
        action: 'delete',
        messageid: messageID
    }).fail(function() {
        alert("There was an error while trying to make this request;  If it persists please contact support");
    });
}​

还有,.success是折旧的,使用.done

编辑:

为了完整起见,您可以使用async:false并存储true或存储false在一个变量中,然后从您的函数中返回它,但是由于async:false暂停代码执行和用户交互,效率较低且不推荐使用。

于 2012-06-21T14:53:42.773 回答