在ajax成功中调用函数

时间:2014-05-31 20:30:28

标签: javascript jquery ajax

我试图从AJAX成功处理程序中调用一个函数,但它被忽略了

$.ajax({
    url: '../ajax/create_audit_standard_carosel.php',
    type:'POST',
    data: 'audit_id='+audit_id+'&user_id='+user_id,
    dataType: 'json',
    success: function(response){
        $('#num').html(response.standards_count);
        $('#standards_list').html(response.output);
            jQuery(function($) {
                $("#selected_standards").touchCarousel({
                   itemsPerPage: 4,             
                   scrollbar: true,
                   scrollbarAutoHide: true,
                   scrollbarTheme: "dark",              
                   pagingNav: false,
                   snapToItems: true,
                   scrollToLast: true,
                   useWebkit3d: true,               
                   loopItems: false         
                });
            });
     }, // End of success function of ajax form
    error:function (xhr, ajaxOptions, thrownError){
      alert(thrownError);
    }
}); // End of ajax call

响应正确,#standards_list的内容使用正确的内容进行修改,因此我知道AJAX调用正常,但成功调用中的函数完全被忽略。

1 个答案:

答案 0 :(得分:1)

如果你的意思是在你更新#standards_list元素之后直接开始的那个函数,那就是因为你试图绑定到一个早已被触发的事件。

将函数传递给全局jQuery函数是绑定到document.ready事件的快捷方式。加载页面时会触发此事件,并且不会因AJAX调用而再次触发该事件。

在更新touchCarousel后,只需删除包装函数并调用#standards_list element方法,例如:

$.ajax({
    url: '../ajax/create_audit_standard_carosel.php',
    type:'POST',
    data: 'audit_id='+audit_id+'&user_id='+user_id,
    dataType: 'json',
    success: function(response){
        $('#num').html(response.standards_count);
        $('#standards_list').html(response.output);
        $("#selected_standards").touchCarousel({
            itemsPerPage: 4,             
            scrollbar: true,
            scrollbarAutoHide: true,
            scrollbarTheme: "dark",              
            pagingNav: false,
            snapToItems: true,
            scrollToLast: true,
            useWebkit3d: true,               
            loopItems: false         
        });
    }, // End of success function of ajax form
    error:function (xhr, ajaxOptions, thrownError){
        alert(thrownError);
    }
}); // End of ajax call`
相关问题