【发布时间】: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