douhui9380 2015-06-23 07:58
浏览 35
已采纳

如何将主文件中写入的函数调用到php和jquery中的ajax文件中

Written a Jquery function in main file (index.php) & want to call same function in ajax file that is called from main file.

  • 写回答

1条回答 默认 最新

  • duanjiong5686 2015-06-23 08:08
    关注

    if the function in the main page and you inject ajax to container inside it, the function need to be available for calling, in the end of the ajax file add function() //main function

    you can also choose another approach after the ajax success you can call the function directly... by passing it as callback for example:

    ajax(url,mainFunction,true)
    
    
    function ajax(url, callback, async) {
        var date = new Date();
        var ticks = date.getTime();
    
        if (url.indexOf('?') == -1)
            url = url + "?tick=" + ticks;
        else
            url = url + "&tick=" + ticks;
    
    
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'html',
            async: async == undefined ? true : async,
            success: function (data) {
    
                if (data != undefined)
                    callback(data);
            },
    
            error: function (xhr, textStatus, errorThrown) {
               //throw error
            },
    
            complete: function () {
    
            }
    
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?