【发布时间】:2011-03-27 12:09:12
【问题描述】:
我有一个页面 (page1.php),我在其中使用选择框将另一个页面 (page2.php) 加载到 DIV 中。在 page2.php 中,有一个 UL,可将数据库中的数据(通过 PHP)加载到 LI 中,并且是可排序的。
我的问题是,当我自己加载 page2.php 时,它可以很好地序列化。但是,当 page2.php 通过 .load() 加载到 page1.php 中时,它根本不会序列化并且我得到未定义。
这是重要的代码,它本身也可以正常工作,但在通过 .load() 函数加载此页面时就不行了
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<style>
#thelist { list-style-type: none; margin: 0; padding: 0; width:700px; }
#thelist li { margin: 3px 3px 3px 0; padding: 1px; float: left; width: 200px; height: 150px; }
</style>
<ul id="thelist">
<li style="margin-bottom:5px;" name='listItem_1' id='listItem_1'>
test1
</li>
<li style="margin-bottom:5px;" name='listItem_2' id='listItem_2'>
test2
</li>
<script type="text/javascript">
$(function() {
$("#thelist").sortable({
update : function () {
var order = $('#thelist').sortable('serialize');
alert(order); // This alerts "undefined" when page2.php is loaded into page1.php via .load();
$("#info").load("reorder_slides.php?"+order);
}});
});
</script>
这是我正在运行的新代码,仍然无济于事。
<script>
$('#edit_service_date').change(function() {
// $(this).val()
$('#editService').slideUp("slow",function(){
$('#thelist').load('page2.php', {id: $('#edit_service_date').val()}, function(){
$("#thelist").sortable({
update : function () {
var order = $('#thelist').sortable('serialize');
alert(order); // This alerts "undefined" when page2.php is loaded into page1.php via .load();
$("#info").load("reorder_slides.php?"+order);
}});
if($('#edit_service_date').val()!="none"){
$('#editService').slideDown("slow");
}
});
});
});
</script>
【问题讨论】:
-
您正在将以上所有内容加载到另一个页面中的 div 中,该页面也有自己的 jQuery 和 ready() 处理程序?
-
你不能使用 $(function(){});在通过 Ajax 检索的 JS 代码内部,但仅在“常规”页面加载时。相反,您需要定义一个从 Ajax 回调的 onComplete 方法调用的新 JS 初始化函数。