Javascript document.ready()只运行一个函数而不是两个函数
我试图在document.ready()中运行两个js函数(我使用的是jquery),但只运行一个。这是我的密码:Javascript document.ready()只运行一个函数而不是两个函数,javascript,jquery,function,document-ready,Javascript,Jquery,Function,Document Ready,我试图在document.ready()中运行两个js函数(我使用的是jquery),但只运行一个。这是我的密码: $(document).ready(function() { show_properties(); table_events(); }); 这些函数写在同一个js文件中,但在加载网站时只加载show_properties()。我在firebug控制台中测试了如何编写 表_事件() 控制台告诉我“未定义”,但在那之后,函数被加载,ajax调用和函数内部的一
$(document).ready(function() {
show_properties();
table_events();
});
这些函数写在同一个js文件中,但在加载网站时只加载show_properties()。我在firebug控制台中测试了如何编写
表_事件()
控制台告诉我“未定义”,但在那之后,函数被加载,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);
});
}
显然,这个函数()在网页加载时不运行;但是当我再次在控制台中写入时
表_事件()
然后,当我在表的tr中单击时,该函数内的代码将运行。如果控制台返回“undefined”,则表示该函数存在,但结果返回undefined
您的函数需要修复,$(document).ready()可能还可以
尝试调用show_properties()
中最后一行的table_events()
,看看这是否有效。需要检查的一些事项:
表_事件是否存在于$(document.ready)的范围内
show_属性是否可能引发错误
这可能是“警报调试”的好例子:
table_events函数中可能有错误。尝试使用简单的警报语句进行调试。是
$('.apartmentos tr')
元素与load
调用一起加载的show\u属性
如果是,则问题是由于执行表事件时,tr元素尚未插入列表aptos
(原因加载
使用ajax,这是异步的)
要查看,请添加
console.log("trs count: ", $('.apartamentos tr').size());
在表的顶部,显示事件功能
要修复此问题,您应该将表\u事件
作为completetion处理程序传递给加载
调用:
$('#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_属性引发了一个错误,阻止了table_事件的执行。但如果您尝试使用console.log(“text”)而不是alert(alert正在阻塞,它将隐藏异步调用中的问题)进行调试,效果会更好
请试着在上做一个例子,您确定在
显示属性中没有任何错误吗?如果颠倒顺序会发生什么?您如何知道表_事件没有运行?您的表_事件()是否运行;函数实际上存在于$(文档)之外。准备好了吗?有javascript错误吗?包含更多内容(代码),因为某些内容似乎已损坏或出错。在show\u properties
中有哪些table\u events
依赖于哪些内容?例如AJAX请求?我在show_properties()的末尾尝试运行它,但仍然没有运行该函数。这是值得研究的,如果使用jQuery的AJAX函数,该函数可以作为“成功”事件的一部分调用。我在table_events()中添加了一些console.log事件,即使打印了控制台日志MSG,加载网页时,不会加载click事件的函数()中的console.log。
console.log("trs count: ", $('.apartamentos tr').size());
$('#lista_aptos')
.html(wait)
.load('properties_list.php',{'user_id':user_id}, table_events);