doutu7123 2010-08-09 20:12
浏览 45
已采纳

在via .load()中加载数据后使用JQuery UI的可排序('serialize');

I have a page (page1.php) where I am using a select box to load in another page (page2.php) into a DIV. Inside page2.php there is a UL that loads data from a database (via PHP) into LIs and the are sortable.

My problem is, when I load page2.php by itself, it serializes fine. However, when page2.php is loaded via .load() into page1.php, it doesn't serialize at all and I get undefined.

Here is the important code, again this works fine by itself, but not when this page is loaded in via the .load() function

 <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> 

This is the new code I am running, still to no avail.

<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>
  • 写回答

1条回答 默认 最新

  • doutuo2829 2010-08-09 20:46
    关注

    If everything you posted above is being brought into another page via .load(), then I see (at least) two problems:

    1. You're loading jQuery and jQuery UI twice: once in the outer page, and once in the inner page loaded via ajax. There's no need.
    2. You're expecting $(function(){}) to fire after being loaded into the "inner" page within the div. $(function(){}) is a synonym for $(document).ready( function(){} ), and in fact the ready event has already fired (when the outer page DOM became ready). It won't do anything here.

    You should try triggering the .sortable() stuff inside the callback of the .load() you're using to bring the inner document into the div:

    /* on page1.php */
    $('#yourdiv').load( 'page2.php', function(){
        $('#thelist').sortable( /* arguments */ );
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。