简体   繁体   English

ajax成功调用函数

[英]call function within ajax success

I am trying to call a function from within an AJAX success handler, but it is being ignored 我正在尝试从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

The responses are correct and the contents of #standards_list are amended with the correct content so I know that the AJAX call is working correctly, but the function within the success call is completely ignored. 响应是正确的,并且#standards_list的内容已修改为正确的内容,因此我知道AJAX调用可以正常工作,但是成功调用中的功能将被完全忽略。

If you mean the function beginning directly after where you update the #standards_list element, it's because you are trying to bind to an event that has long since fired. 如果您的意思是该函数在更新#standards_list元素之后立即开始,那是因为您试图绑定到触发了很长时间的事件。

Passing a function to the global jQuery function is a shortcut for binding to the document.ready event. 将函数传递给全局jQuery函数是绑定到document.ready事件的快捷方式。 This event is fired when the page loaded and will not fire again as a result of the AJAX call. 加载页面时将触发此事件,并且不会因AJAX调用而再次触发。

Just remove the wrapping function and call the touchCarousel method after updating the #standards_list element , eg: 只需删除包装函数并在更新#standards_list element后调用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`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM