document.ready()只运行一个函数而不是两个函数

时间:2013-04-16 15:19:40

标签: javascript jquery function document-ready

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

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

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

  

table_events()

并且控制台告诉我“undefined”,但之后函数被加载并且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时,此函数内的代码就会运行。

4 个答案:

答案 0 :(得分:1)

$('.apartamentos tr')load调用show_properties来加载table_events个元素吗?

如果是,那么问题是由于当#lista_aptos执行时,tr元素尚未插入load(因为console.log("trs count: ", $('.apartamentos tr').size()); 使用ajax,这是异步的)。

要检查请添加

table_events

在table_events函数之上。

要解决此问题,您应该将load作为完成处理程序传递给$('#lista_aptos') .html(wait) .load('properties_list.php',{'user_id':user_id}, table_events); 来电:

{{1}}

旧回复

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

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

如果你在ajax响应处理程序中定义了table_events函数,那么如果你把它放在可以从引用函数看到的作用域中,那么在table_events定义之前可能会调用table_events。

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

请尝试在http://jsfiddle.net/

上做一个例子

答案 1 :(得分:0)

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

你的功能需要修复,$(文件).ready()可能没问题。

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

答案 2 :(得分:0)

要检查的一些事项:

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');
});

答案 3 :(得分:0)

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