【问题标题】:document.ready() just running one function instead of twodocument.ready() 只运行一个函数而不是两个
【发布时间】:2013-04-16 15:19:39
【问题描述】:

我试图在 document.ready() 中运行两个 js 函数(我正在使用 jquery),但只运行一个。这是我的代码:

    $(document).ready(function() {
    show_properties();
    table_events();
 });

函数写在同一个js文件中,但是网站加载时只加载show_properties()。我在firebug控制台中测试写了

table_events()

控制台告诉我“未定义”,但之后该函数被加载,ajax 调用以及该函数内的所有内容都开始工作。

这是为什么呢?我该如何解决这种行为?

谢谢。

这是我要运行的函数:

    function table_events(){
 $.ajaxSetup ({  
         cache: false  
     });  

 var wait = "<img src='../images/loading_red_transparent.gif' alt='loading...' style='display:block;margin-left:auto;margin-right:auto;'/>";    

    $('.apartamentos tr').on('click', function() {
            alert("hola.")
               var id_propiedad = $(this).find(".id_propiedad").html();   
           var nombre_propiedad = $(this).find(".nombre_propiedad").html(); 
       //$('#property_information').hide('slow');

       $("#property_information")
       .html(wait)
       .load('properties_info.php',{'nombre_propiedad':nombre_propiedad,'id_propiedad':id_propiedad});
       //$('#property_information').show('slow');

        }); 


 }


 function show_properties(){
 $.ajaxSetup ({  
         cache: false  
     }); 
 var wait = "<img src='../images/loading_red_transparent.gif' alt='loading...' style='display:block;margin-left:auto;margin-right:auto;'/>";

     $('#busca_propiedades').on('click',function(){

         var user_id = $('#user_list').val();

         /*var data = $.ajax({
              url: "properties_list.php",
              type: 'POST',
              data:{ 'user_id': user_id },
              cache: false,
              async: false
             }).responseText;

         });*/

         $('#lista_aptos')
         .html(wait)
         .load('properties_list.php',{'user_id':user_id});

        //alert(data);
         });

 }

编辑: 在使用 console.log 进行一些调试后,我发现这段代码是网页加载时没有执行的代码:

$('.apartamentos tr').on('click', function() {
            alert("hola.")
               var id_propiedad = $(this).find(".id_propiedad").html();   
           var nombre_propiedad = $(this).find(".nombre_propiedad").html(); 
       //$('#property_information').hide('slow');

       $("#property_information")
       .html(wait)
       .load('properties_info.php',{'nombre_propiedad':nombre_propiedad,'id_propiedad':id_propiedad});
       //$('#property_information').show('slow');

        }); 

显然,这个函数()是网页加载时不运行的函数;但是当我再次在控制台中写入时

table_events()

然后当我点击表格的 tr 时,这个函数内部的代码就会运行。

【问题讨论】:

  • 你确定show_properties'没有任何错误吗?
  • 如果您颠倒订单会发生什么?你怎么知道 table_events 没有运行?
  • 你的 table_events();函数实际上存在于 $(document).ready 之外吗?
  • 任何 javascript 错误?包含更多内容(代码),因为某些内容似乎已损坏或错误。
  • show_properties 中有什么东西是table_events 所依赖的吗?比如AJAX请求?

标签: javascript jquery function document-ready


【解决方案1】:

$('.apartamentos tr') 元素是否通过 load 调用在 show_properties 中加载?

如果是,那么问题是由于在执行table_events 时,tr 元素尚未插入#lista_aptos(因为load 使用ajax,这是异步的)。

要检查请添加

console.log("trs count: ", $('.apartamentos tr').size());

在您的 table_events 函数之上。

要修复,您应该将 table_events 作为完成处理程序传递给 load 调用:

$('#lista_aptos')
         .html(wait)
         .load('properties_list.php',{'user_id':user_id}, table_events);

旧响应

你说“......之后函数被加载并且 ajax 调用......”

请记住,ajax 调用是异步的。

如果您在 ajax 响应处理程序中定义 table_events 函数,并且如果您将其置于引用函数可见的范围内,则可能会在 table_events 定义之前尝试调用 table_events。

或者,正如 Raghavan 所说,show_properties 引发了一个错误,阻止了 table_events 的执行。但是如果你尝试使用 console.log("text") 而不是 alert 进行调试会更好(alert 是阻塞的,它会隐藏异步调用的问题)

请尝试在http://jsfiddle.net/上做一个例子

【讨论】:

  • 这是值得研究的,如果正在使用jQuery的AJAX函数,该函数可以作为“成功”事件的一部分被调用。
  • 我在 table_events() 中添加了一些 console.log 事件,尽管打印了控制台日志消息,但 click 事件的 function() 中的 console.log 在单击事件时不会加载网页加载。
【解决方案2】:

如果控制台返回“未定义”,则表示该函数存在,但结果返回未定义。

你的函数需要修复,$(document).ready() 可能没问题。

尝试在show_properties() 的最后一行调用table_events(),看看是否可行。

【讨论】:

  • 我试过在show_properties()的最后运行,还是没有运行函数
【解决方案3】:

需要检查的几件事:

table_events 是否存在于 $(document).ready 的范围内?

show_properties 是否可能引发错误?

这可能是“警报调试”的好案例:

$(document).ready(function() {
    show_properties();
    // You might also put an alert at the very end of show_properties.
    alert('show_properties called; now calling table_events()');
    table_events();
    alert('table_events called; document.ready done');
});

【讨论】:

    【解决方案4】:

    table_events 函数中可能存在错误。尝试使用简单的警报语句进行调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多