2013-06-26 46 views
1

关于“如何在Ajax载入内容后运行js”有两个以上的问题。他们都建议在Ajax加载完成后运行JS。我知道如何处理。是否有任何js函数在ajax加载内容后运行脚本?

假设超过40页通过Ajax加载页面my-Page.php。在Ajax加载内容后,是否有像$(document).ready这样的JS函数来处理运行脚本?

请注意,编辑超过40个文件是不合理的。

多一点点关于我的问题:

我定制糖CRM,而不是使用其简单的JS日历jQuery UI的日期选择器。在Ajax调用中调用$('#inp').datepicker()失败,因为没有$(#inp)对象可用。

工作

Calendar.setup ({ldelim} 
     inputField : "{{$idname}}", 
     ifFormat : "{$CALENDAR_FORMAT}", 
     daFormat : "{$CALENDAR_FORMAT}", 
     button : "{{$idname}}_trigger", 
     singleClick : true, 
     dateStr : "{$date_value}", 
     step : 1, 
     weekNumbers:false {rdelim} ); 

doesn't work

jQuery(function() {ldelim} 
         jQuery('#{{$idname}}_hpm').datepicker({ldelim} 
          showOn: 'button', 
          buttonImage: 'include/jdp/styles/images/calendar.png', 
          buttonImageOnly: true, 
          changeMonth: true, 
          changeYear: true, 
          dateFormat: 'yy/mm/dd' 
         {rdelim}); 
        {rdelim}); 

我不知道它是如何的基本Calendar.setup()作品,但我的脚本。

+2

我希望你只有一个常用的函数来加载所有40个文件中使用的ajax内容? – Bergi

+3

“请注意,编辑超过40个文件是不合理的。”哇。 – Brad

+0

肯定有可能有一个共同的功能,但这个功能服务于许多其他的Ajax请求。好评@Brad,但有没有可用的功能? – HPM

回答

1

我也遇到过这个问题。解决方案暗示在here

由于SugarCRM附带YUI库,因此您可以使用onContentReady函数。

var yourFunction = function(){ 
    console.log("Hello world! #myElementId is loaded!") 
}; 
YAHOO.util.Event.onContentReady('myElementId',yourFunction); 
0

还有就是AJAX调用将运行它内部的任何功能的“成功”部分:

 
$.ajax({ 
type : 'POST', 
      url : 'example.org', 
      dataType : 'json', 
      data: { id:'33d', 
     }, 
     success : function(data){ 
      //Add function here 
     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown) { 
      // Add error functions here 
     }); 
     return false; 
     }); 

我不知道如果这是你在找什么,但。

相关问题