【问题标题】:TypeError: $(...) not a function类型错误:$(...) 不是函数
【发布时间】: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>

我根据遇到的错误进行了多次搜索,但似乎无法解决问题。任何帮助将不胜感激。

【问题讨论】:

标签: php javascript jquery typeerror


【解决方案1】:

首先,在 document.ready 中包含 $.fn.deleterow = function(event) { 是没有意义的。您应该将其移出该方法。

我个人会将代码更改为不依赖表中的内联事件处理程序。您正在使用 jQuery,因此请使用它。使用事件冒泡对您有利。

将其添加到表级别并侦听具有 id 的 td 上的点击事件。

$("table tbody").on("click", "td[id]", function(e){
   if (!confirm('Are you sure you want to delete this student?')) {
      return false;
  }
  var id = this.id;
  $.ajax({
          url: 'delete.php',
          type: 'GET',
          data: 'id=' + id,
      });
  $(this).parent().css('display', 'none'); 
});

jsFiddle

【讨论】:

    【解决方案2】:

    您最好使用 jQuery 的处理程序而不是 onclick 来绑定这些事件,这是不好的做法。例如:

    $('#myTable').on('click', 'td', function(e) {
        $(this).deleterow(e);
    });
    

    【讨论】:

      【解决方案3】:

      这是因为:

      <td onclick='$(this).deleterow(event);'
      

      由于封闭的“”,JavaScript 无法将其视为函数。

      我建议这样做:

      <td class='deleterow'>
      

      然后

      $(body).on('click', '.deleteRow', function(event) {
          $(this).deleterow(event);
      });
      

      【讨论】:

        【解决方案4】:

        尝试改写成常用的javascript函数

        function deleterow(id) {...}
        

        在php中

        echo "<tr><td>".$row['id']. "</td><td>". $row['last']. "</td><td>". $row['first']."</td><td>". $row['assignment']."</td><td>". $row['assignteacher']."</td><td onclick='deleterow(".$row['id']. ")' id='".$row['pk']."'>Delete</td></tr>";
        

        【讨论】:

          【解决方案5】:

          测试 jQuery 是否在控制台中加载(检查 jQuery 变量)...

          你应该这样包装你的代码:

          (function( $ ){
                $(document).ready(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 );
          

          但最重要的是检查 jQuery 是否正确加载并解决它...

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 2016-02-26
            • 2016-03-06
            • 2018-04-28
            • 2019-02-14
            • 2021-10-02
            • 2021-10-30
            • 2018-11-22
            • 2017-10-12
            相关资源
            最近更新 更多