javascript - Uncaught ReferenceError $ is not defined after new function

标签 javascript jquery

我真的需要找到这个错误的根源。前几天我临时解决了它,但似乎每次我在 JS 文件中添加一个新函数时我都会得到它。之前我只会在新函数上得到“Uncaught ReferenceError $ is not defined”,但现在它适用于所有函数。我还收到“ncaught SyntaxError: Unexpected token <”,但至少目前这是次要问题。

索引文件

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-draw.js"></script>
</head>
<h2>Players</h2>
<div class="players">
<script>
  setInterval( refreshPlayers, 5000 );
  var inRequest = false;
</script></div>
<h3>Please select the Game you would like to view</h3>
<div class="pickGame">
<script>
  setInterval( viewGames, 5000 );
  var inRequestT = false;
</script>
</div>
<h2>Game</h2>
<div class="agonas">
<script>
  setInterval( refreshAgonas, 5000);
  var inRequestG = false;
</script></div>
</html>

jquery-draw.js文件

function viewGames(){
    if ( inRequestT ) {
      return false;
    }
    inRequestT = true;
    var load = $.get('drawgames.php');
    $(".pickGame").html('Refreshing');
    load.error(function() {
      console.log("Mlkia kaneis");
      $(".pickGame").html('failed to load');
      // do something here if request failed
    });
    load.success(function( res ) {
      console.log( "Success" );
      $(".pickGame").html('<form method="post" action="viewgame.php">
<select name="gameNo">'+res+'</select>
<input type="submit" value="Choose Game">
</form>');
    });
    load.done(function() {
      console.log( "Completed" );
      inRequestT = false;
    });
}
function refreshAgonas() {
    if ( inRequestG ) {
      return false;
    }
    inRequestG = true;
    var load = $.get('playersdata.php');
    $(".agonas").html('Refreshing');
    load.error(function() {
      console.log("Mlkia kaneis");
      $(".agonas").html('failed to load');
      // do something here if request failed
    });
    load.success(function( res ) {
      console.log( "Success" );
      $(".agonas").html('<table border="1"><tr><th>ID</th><th>Name</th><th>Email</th><th>League</th><th>Sex</th><th>Birthday</th></tr>'+res+'</table>');
    });
    load.done(function() {
      console.log( "Completed" );
      inRequestG = false;
    });
  }
function refreshPlayers() {
    if ( inRequest ) {
      return false;
    }
    inRequest = true;
    var load = $.get('playersdata.php');
    $(".players").html('Refreshing');
    load.error(function() {
      console.log("Mlkia kaneis");
      $(".players").html('failed to load');
      // do something here if request failed
    });
    load.success(function( res ) {
      console.log( "Success" );
      $(".players").html('<table border="1"><tr><th>ID</th><th>Name</th><th>Email</th><th>League</th><th>Sex</th><th>Birthday</th></tr>'+res+'</table>');
    });
    load.done(function() {
      console.log( "Completed" );
      inRequest = false;
    });
  }

最佳答案

问题出在这里:

      $(".pickGame").html('<form method="post" action="viewgame.php">
<select name="gameNo">'+res+'</select>
<input type="submit" value="Choose Game">
</form>');

JavaScript 不允许在字符串中使用原始换行符,除非您使用反斜杠对它们进行转义。如果您有它们,则无法解析整个脚本,并且什么都不起作用。

单独地,您正在执行此操作:

var load = $.get('drawgames.php');

然后在 load 上调用函数 errorsuccessdoneload 将引用一个 jqXHR 对象。它们没有errorsuccess 函数(它们有done 函数)。您混淆了 $.ajax 函数接受回调的属性jqXHR 提供的函数。我推荐reviewing the documentation了解使用 $.get 的正确方法。

关于javascript - Uncaught ReferenceError $ is not defined after new function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966768/

相关文章:

jquery - 有没有免费的 jquery 幻灯片可以做到这一点?

javascript - 如何将 JSON 数据放入 jQuery/Javascript 数组中以供以后使用?

javascript - 如果选中,则 jQuery 计算总和文本字段

javascript - HTML5 中自定义数据属性名称中的有效字符

javascript - 如何模拟 float :top in javascript

IE 中的 jQuery 鼠标闪烁

javascript - 更改react-chartjs 2中的工具提示方向

javascript - .htaccess 或 JavaScript 重定向

javascript - 使用绑定(bind)原型(prototype)更改 'this' 的范围

javascript - DIV 的最大长度,例如 Textarea