jquery重新加载后,点击不起作用
链接打开下拉菜单以选择排序操作。排序后,页面通过 jquery load 加载到自身,并且下拉列表关闭。我们可以再做一次。之后,打开下拉菜单的链接不再起作用,下拉菜单保持关闭状态。
如何解决这个问题?主要目标是拥有一个用于对动态表进行排序的工作(打开和关闭)下拉菜单。 (下拉也是动态的,所以需要用ajax请求加载)
<div id="wrapper">
<!-- THIS IS DROPDOWN -->
<div id="sortdropdown">
<p><a href="" id="sortn">Name</a><br /><a href="" id="sortd">Date</a></p>
</div>
<!-- THIS IS LINK TO OPEN DROPDOWN -->
<p><a href="" id="sortbutton">Sort by</a></p>
</div>
<script type="text/javascript">
function doLoad(sort){
var selector = "div#wrapper";
$(selector).load('indexsort.php?act='+sort+' '+selector, function(){
$('div#sortdropdown').hide();
doBindings();
});
}
function doBindings(){
//sorts table on click - sorts by name
$('a#sortn').click(function(event) {
event.preventDefault();
doLoad('sn');
});
//sorts table on click - sorts by date
$('a#sortd').click(function(event) {
event.preventDefault();
doLoad('sd');
});
//opens/closes sort by dropdown menu
$('a#sortbutton').click(function(event) {
event.preventDefault();
$('div#sortdropdown').toggle();
});
}
$(document).ready(function(){
//binds clicks so they are active after load method
doBindings();
//hides dropdown after landing
$('div#sortdropdown').hide();
});
</script>
Link opens dropdown to choose sorting action. When sorted, page loads into itself with jquery load, and dropdown is closed. We can do that one more time. After that, link which opens dropdown doesn't work anymore and dropdown stays closed.
How to fix this? The main goal is to have a working (opening and closing) dropdown menu for sorting dynamic table. (dropdown is also dynamic, so it needs to be loaded with ajax request)
<div id="wrapper">
<!-- THIS IS DROPDOWN -->
<div id="sortdropdown">
<p><a href="" id="sortn">Name</a><br /><a href="" id="sortd">Date</a></p>
</div>
<!-- THIS IS LINK TO OPEN DROPDOWN -->
<p><a href="" id="sortbutton">Sort by</a></p>
</div>
<script type="text/javascript">
function doLoad(sort){
var selector = "div#wrapper";
$(selector).load('indexsort.php?act='+sort+' '+selector, function(){
$('div#sortdropdown').hide();
doBindings();
});
}
function doBindings(){
//sorts table on click - sorts by name
$('a#sortn').click(function(event) {
event.preventDefault();
doLoad('sn');
});
//sorts table on click - sorts by date
$('a#sortd').click(function(event) {
event.preventDefault();
doLoad('sd');
});
//opens/closes sort by dropdown menu
$('a#sortbutton').click(function(event) {
event.preventDefault();
$('div#sortdropdown').toggle();
});
}
$(document).ready(function(){
//binds clicks so they are active after load method
doBindings();
//hides dropdown after landing
$('div#sortdropdown').hide();
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将绑定移出 doBindings() 函数,并使用 jQuery 的 live() 函数:
另外,更改你的选择器。
a#sortbutton
的效率比简单的#sortbutton
低,如 jQuery API:Move the bindings out of the doBindings() function, and use jQuery's live() function:
Also, change your selectors.
a#sortbutton
is less efficient than simply#sortbutton
, as described in the jQuery API:我认为您需要在调用indexsort.php文件之前取消绑定 doLoad函数中的点击事件。
我不会推荐live,因为它非常昂贵。
I think you need to unbind the click events in doLoad function before calling the indexsort.php file.
I won't recommend live because its very expensive method.