【发布时间】:2013-04-01 01:30:26
【问题描述】:
我使用 jQuery 创建了一个页面,用户可以在该页面中单击表中显示“删除”的单元格,它会发送一个 ajax 请求以根据单元格的 ID 从数据库中删除该条目然后它会改变 CSS 来隐藏单元格。
我在创建/调整 jQuery 代码时创建了一个测试页面。此页面完美运行。代码如下:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
(function( $ ){
$.fn.deleterow = function(event) {
if (!confirm('Are you sure you want to delete this student?')) {
return false;
}
var id = event.target.id;
$.ajax({
url: 'delete.php',
type: 'GET',
data: 'id=' + id,
});
$(this).parent().css('display', 'none');
}
})( jQuery );
});
</script>
</head>
<table border="1">
<tr>
<td>Cell 2, Row 2</td><td onclick="$(this).deleterow(event);" id="13376">delete</td>
</tr>
</table>
<html>
现在我正在努力让代码在将要使用的实际页面中运行。该页面有一个简短的表单,用户可以在其中选择他们的姓名和日期。此表单发送一个 ajax 请求,该请求在 div 中返回结果。返回的数据是一个 table ,这是我试图让我的函数工作的地方。该表附加了一个 tablesorter 脚本,还附加了我的函数。
tablesorter 仍然可以正常工作,但是当我单击其中包含“删除”的单元格时没有任何反应。我使用 FireBug 来查看问题,它给了我错误,“TypeError: $(...).deleterow is not a function”
这里是用户提交表单的主页的代码,结果在 div 中加载:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript' src='jquery.tablesorter.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
$('#myTable').tablesorter();
}
);
</script>";
</head>
<body id="my-students">
<?php include 'header.php'; ?>
<?php include 'nav-tutoring.php'; ?>
<div id="content">
This is where you can view students whom you have assigned to tutoring.
<p>
You may change the way each column is sorted by clicking on the column header.
<p>
<b>Select your name and the date to view students you have assigned for that day.</b>
<form> My form is here; removed to make post shorter </form>
<script>
$('#submit').click(function()
{
$.ajax({
type: 'POST',
url: "mystudents.php",
data: $('#name').serialize(),
success: function(data) {
$("#list").empty();
$('#list').append(data);
}
});
return false;
});
</script>
<div id="list"></div>
这是插入到表单下方 div 中的页面代码。这是 tablesorter 工作的页面,但我无法让我的功能工作。我还确保将这些脚本库包含在该 div 所在的主页头中。
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
<script type='text/javascript' src='jquery.tablesorter.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
$('#myTable').tablesorter();
(function($) {
$.fn.deleterow = function(event) {
if (!confirm('Are you sure you want to delete this student?')) {
return false;
}
var id = event.target.id;
$.ajax({
url: 'delete.php',
type: 'GET',
data: 'id=' + id,
});
$(this).parent().css('display', 'none');
}
})(jQuery);
});
</script>
<table id='myTable' class='tablesorter' border="1">
<thead><tr><th>ID Number</th><th>Last</th><th>First</th><th>Tutoring<br>Assignment</th><th>Assigning Teacher</th><th>Delete?</th></tr></thead><tbody>
<?php
while($row = mysql_fetch_array($data)){
echo "<tr><td>".$row['id']. "</td><td>". $row['last']. "</td><td>". $row['first']."</td><td>". $row['assignment']."</td><td>". $row['assignteacher']."</td><td onclick='$(this).deleterow(event);' id='".$row['pk']."'>Delete</td></tr>";
}
?>
</tbody></table>
我根据遇到的错误进行了多次搜索,但似乎无法解决问题。任何帮助将不胜感激。
【问题讨论】:
-
如果您在页面加载时正确下载了 jQuery 脚本,请在控制台中检查。
-
您应该为您的
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">添加一个type="text/javascript"属性
标签: php javascript jquery typeerror