【发布时间】:2011-12-28 21:24:55
【问题描述】:
他们主页上的演示表与我使用的几乎完全相同(特别是分页),除了每一行都有一个可以点击的区域:
<a href="#" class="show-post"><%= Post.title %></a>
此链接会打开一个 jquery UI 模式对话框,其中显示一些 ajax 请求的信息。
第 1 部分(已解决),请参阅下面的第 2 部分
我正在尝试运行一个在第一页上正常工作的 onclick 事件,但是一旦我转到第 2 页(或任何其他页面),它就会停止工作。我检查了源代码以确保它实际上没有在所有代码中做任何有趣的事情(所有行,甚至是被分页隐藏的行)
有什么想法吗?
$(function() {
$('#dialog').dialog({
autoOpen: false,
resizable: false,
maxHeight: 600,
width: 650,
modal: true,
beforeClose: function close() {
$('#dialog').html('');
}
});
$('.show-post').click(function() {
clickLink(this);
return false;
});
});
感谢回答我问题的人!我解决了这个问题。
第 2 部分
我想开始工作的下一个“问题”ID 是...我正在使用左右箭头键让他们“扫描”到下一行或上一行,并显示信息。这与关闭它然后必须单击下一个相反。
我想这样当您到达第一页的底部或第二页的顶部时,分别隐藏下一个/上一个将自动加载该页面,转到顶部(或底部),然后打开该页面另一页上该行的对话框。
这是我的点击功能(我知道它的结构可能不是最好的......我是 jquery 的新手)
$(document).ready(function() {
oTable = $('#posts').dataTable({
"bJQueryUI": true,
"iDisplayLength": 400,
"bAutoWidth": false,
"sPaginationType": "full_numbers",
"aLengthMenu": [[-1, 400, 100, 50], ["All", 400, 100, 50]]
});
$(this).keydown(function(e) {
var id = $("#dialog").attr("data-id");
currentPost = $("#posts tr[data-id=" + id + "]");
if (e.keyCode == 39 && $('#dialog').html() != "") {
/* Remove current background */
$(currentPost).blur()
$(currentPost).removeClass("current");
$(currentPost).find("td.sorting_1").removeClass("current");
var next = currentPost.next().find(".show-post");
clickLink(next);
} else if (e.keyCode == 37 && $('#dialog').html() != "") {
/* Remove current background */
$(currentPost).removeClass("current");
$(currentPost).find("td.sorting_1").removeClass("current");
var prev = currentPost.prev().find(".show-post");
clickLink(prev)
}
});
});
这是实际的点击功能
function clickLink(src) {
var post = $(src);
var id = $(post).parent().parent().attr('data-id');
/* Set background for current line */
$(post).parent().parent().find("td.sorting_1").addClass("current");
$(post).parent().parent().addClass("current");
$('#dialog').attr("data-id", id);
$('#dialog').load('/show-post/' + id, function() {
$.ajax({
type: "POST",
url: "/checkstatus/" + id,
dataType: "html",
error: function(data){
$("#dialog").fadeOut("fast", function() {
$("#dialog").html("<img src='/img/invalid.jpg' alt='invalid' style='margin: 40px auto; display: block;'>").fadeIn("slow");
});
}
});
/* Set Visited */
$(post).parent().parent().removeClass("visited").addClass("visited");
$('#dialog').dialog({
title: post.html(),
beforeClose: function close() {
$(post).parent().parent().find("td.sorting_1").removeClass("current");
$(post).parent().parent().removeClass("current");
},
buttons: {
"Email 1": function() {
$.ajax({
type: "POST",
url: "/get-email/" + id + "/" + "1",
dataType: "html",
success: function(data) {
window.location.href = data + "&subject=" + post.html();
}
});
},
}
});
$('#dialog').dialog('open');
});
return false;
};
【问题讨论】:
-
听起来您的分页在 dom 更改后没有重新附加点击处理程序。尝试重新附加事件或使用附加事件的动态形式,例如委托或实时(取决于您使用的 jquery 版本)。
标签: javascript jquery datatables