2011-05-09 63 views
3

有两个链接排序,SortName,SortDate。当使用jquery load来加载表($('table.listing').load ...)时,它就起作用了。当使用$('form')。load ...那么它不起作用。这是为什么?jquery ajax加载链接的问题

下面的代码有效,但如果将'table.listing'更改为'form',则不起作用。问题是因为链接也应该加载,并且它们位于表格上方的div中,所以我需要使用'form'或div,尽管div wrapper也不起作用。

这是什么意思,它不工作:如果你使用'表单',你需要点击链接TWICE容器加载!?

<form method="post"> 
<div> 
    <a href="" id="sortn">SortName</a><br/> 
    <a href="" id="sortd">SortDate</a> 
</div> 
<table class="listing"> 
    ...table code here 
</table> 
</form> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('a#sortn').click(function(event) { 
     event.preventDefault(); 
     $('table.listing').load('index.php?sort=1 table.listing'); 
    }); 
    $('a#sortd').click(function(event) { 
     event.preventDefault(); 
     $('table.listing').load('index.php?sort=2 table.listing'); 
    }); 
}); 
</script> 
+1

这是不正确的:'$( 'table.listing')负载( 'index.php文件排序= 1个table.listing?')。 ;'我不确定你在做什么 – Neal 2011-05-09 14:13:08

+0

我使用加载方法将页面的一部分加载到本身。是?sort = 1问题? – Marko 2011-05-09 14:15:38

+0

为什么你在末尾有'table.listing'? – Neal 2011-05-09 14:17:47

回答

2

我不知道为什么加载表单不起作用,但我不会那样做。你应该将你的表单封装在一个div中,然后将所有内容加载到那里。加载之后,您需要重新绑定您的点击操作。什么不关于包装它在一个div?

编辑:重新绑定链接装载完成后:

<script type="text/javascript"> 

    function doLoad(sort){ 
     //if you switch to the div method, obvously the selector needs to change 
     var selector = "table.listing"; 
     //var selector = "#newDivId"; 

     $(selector).load('index.php?sort='+sort+' '+selector, function(){ 
      doBindings(); 
     }); 
    } 

    function doBindings(){ 
     $('a#sortn').unbind('click'); 
     $('a#sortd').unbind('click'); 
     $('a#sortn').click(function(event) { 
      event.preventDefault(); 
      doLoad(1); 
     }); 
     $('a#sortd').click(function(event) { 
      event.preventDefault(); 
      doLoad(2); 
     }); 
    } 


$(document).ready(function(){ 
    doBindings(); 
}); 
</script> 
+0

也许 - 重新绑定链接。怎么做? – Marko 2011-05-09 14:21:58

+0

我会更新我的答案 – 2011-05-09 14:24:24

+0

已更新。不知道这是否会解决它。为什么你需要重新加载链接?他们改变了吗?如果没有,你可能会考虑不加载它们。 – 2011-05-09 14:41:13

1

$ .load语法是:

$(<selector>).load(url, formData, function(response, status)); 

上面的选择就是从URL加载的页面将被渲染(在目标)。 formData和回调函数都是可选的。如果您需要将表单数据传递到您的后端应用程序,你可以使用:

$(<selector>).load(url,$("#formid").serialize()); 

我可能是错的,但你正在使用的网址可能不正确(的index.php排序= 1个table.listing?) 。

最后一点:为了加快页面处理速度,使用id而不是类。在您的例子:

<table class="listing" id="data"> 
.... 

,并在Javascript:

$("#data").load(...) 
+0

伟大的建议,我会把他们......在这个问题上,它似乎是关于加载的链接,然后需要重新绑定 – Marko 2011-05-09 14:39:56