我想通过 tablesorter 插件将过滤的记录写入 csv 格式的外部文件。我正在关注Tablesorter 插件的创建者Mottie的这个答案。在 FireBug 错误控制台中,我收到错误消息
TypeError: $(...).on 不是函数 $('.export').on('click', function(){
这是我的文件,使用 tablesorter 以 csv 格式提取记录,
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%
ArrayList<ArrayList<String>> resultsetlist = (ArrayList<ArrayList<String>>) request.getAttribute("SearchRecordsList");
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Research Records</title>
<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<!-- Demo stuff -->
<link class="ui-theme" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.min.js"></script>
<link href="js/tablesorter/docs/css/prettify.css" rel="stylesheet">
<script src="js/tablesorter/docs/js/prettify.js"></script>
<script src="js/tablesorter/docs/js/docs.js"></script>
<!-- Tablesorter: required -->
<link rel="stylesheet" href="js/tablesorter/css/theme.blue.css">
<script src="js/tablesorter/js/jquery.tablesorter.js"></script>
<script src="js/tablesorter/js/jquery.tablesorter.widgets.js"></script>
<script>
$(function() {
$('table').tablesorter({
theme : 'blue',
widgets: ['zebra', 'filter' ]
});
});
$('.exportcsv').on('click', function(){
var csv = [];
// find only visible rows; we're ignoring filtered/hidden rows
$('table').find('tbody tr:visible').find('td').each(function(){
alert("Value of text" + $(this).text());
csv.push( $(this).text());
});
// do what you want with the csv data here
$('textarea').val( csv.join(',') )
});
</script>
<link rel="stylesheet" type="text/css" href="stylesheet1.css">
<title>JSP Page</title>
</head>
<body>
<table class="tablesorter" id="tablesorter-id-variable">
<thead>
<tr>
<%
int index = 0;
String s = "null";
Iterator itrcol = resultsetlist.iterator();
if (itrcol.hasNext()) {
ArrayList<String> col_record = (ArrayList<String>) itrcol.next();
for (index = 0; index < col_record.size(); index++) {
s = col_record.get(index);
%>
<th>
<% out.println(s);%>
</th>
<%
} // End of -for-
%>
</tr>
<%
} //end if
%>
</thead>
<tbody>
<tr>
<%
Iterator itr = resultsetlist.iterator();
itr.next();
while (itr.hasNext()) {
ArrayList<String> each_record = (ArrayList<String>) itr.next();
for (index = 0; index < each_record.size(); index++) {
s = each_record.get(index);
%>
<td>
<% out.println(s);%>
</td>
<%
} // End of -for-
%>
</tr>
<%
} //end while
%>
</tbody>
</table>
<button class="exportcsv">export csv</button><br>
<textarea cols="40" rows="10"></textarea>
</body>
</html>
上面的代码中可能出现什么错误?提前致谢 :)
更新:解决方案
两个答案都是对的!很遗憾我只能接受一个:(
问题是我使用的 Jquery 版本与 1.4 一样旧。所以将它升级到最新的 google cdn's - 1.8 ,解决了这个问题。感谢答案:)