Javascript/JQuery - Chrome 意外令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11759142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript/JQuery - Chrome unexpected token
提问by Peter Austin
Am following a web developer course on-line and one of the projects is to create a fictional stock trading site.
我正在在线学习 Web 开发人员课程,其中一个项目是创建一个虚构的股票交易网站。
I am using jQuery and the Validator plugin on my lookup page, but when I load it in Chrome, I keep getting Uncaught SyntaxError: Unexpected token messages, sometimes for ":", other times its "<".
我在查找页面上使用 jQuery 和 Validator 插件,但是当我在 Chrome 中加载它时,我不断收到 Uncaught SyntaxError: Unexpected token messages,有时是“:”,有时是“<”。
I have looked through similar entries on the site, and tried displaying all chars and checked for matching brackets and braces (notepad++) to no avail, and have gone through the code again and again to no avail before asking for help.
我查看了网站上的类似条目,并尝试显示所有字符并检查匹配的括号和大括号(记事本++)无济于事,并且在寻求帮助之前一遍又一遍地检查代码无济于事。
The page code is below. I would really appreciate any pointers. Hopefully I haven't missed anything silly but ...
页面代码如下。我真的很感激任何指示。希望我没有错过任何愚蠢的事情,但是......
<?
require("../includes/common.php");
$userID = $_SESSION['id'];
$userQuery = $dsn->prepare("SELECT balance FROM cs75f_users WHERE uid=:uidQ");
$userQuery->bindParam(":uidQ",$userID);
$userQuery->execute();
$userData = $userQuery->fetch();
$userBalance = $userData[0];
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="javascript/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="javascript/jquery.validate.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>
<script type="text/javascript"><![CDATA[
$(document).ready(function()
{
$("#search").select();
$("#infoDiv").slideUp();
$("#buyDiv").slideUp();
var searchVal = $("#searchForm").validate({
rules:{
search:{
required:true,
minlength:4,
maxlength:4
},
},
messages:{
search:{
required:"Please enter a valid stock symbol",
minlength:"Please enter a valid 4 character stock symbol",
maxlength:"Please enter a valid 4 character stock symbol"
},
},
errorPlacement: function(error,element)
{
if (element.is(":radio"))
{
error.appendTo(element.parent().next().next() );
}
else if (element.is(":checkbox"))
{
error.appendTo(element.next() );
}
else
{
error.appendTo(element.parent().next() );
}
}
});
$("#searchForm").submit(function() {
$.ajax({
url:"getquote.php",
data:{
symbol:$("#search").val()
}
success: function(data) {
$("#name").html(data.name);
$("#price").html(data.price);
$("#high").html(data.high);
$("#low").html(data.low);
$("#infoDiv").slideDown();
$("#buyDiv").slideDown();
$("#qty").select();
$("#hSymbol").val(data.symbol);
}
});
return false;
});
var buyVal = $("#buyForm").validate({
rules:{
qty:{
required:true,
min:1,
max:10000,
digits:true
},
},
messages:{
qty:{
required:"Please enter a valid quantity",
min:"You must purchase at least one share",
max:"Upper limit of 10,000 shares at one time",
digits:"Please enter a valid number"
},
},
errorPlacement:function(error,element) {
if (element.is(":radio"))
{
error.appendTo(element.parent().next().next() );
}
else if (element.is(":checkbox"))
{
error.appendTo(element.next() );
}
else
{
error.appendTo(element.parent().next() );
}
}
});
});
]]></script>
<title>CS75 Finance Login</title>
</head>
<body>
<div id="logo" align="center">
<img src="images/logo.gif" border="0" alt="CS75 Finance" />
</div>
<div id="welcome" align="center">
<h4>Welcome to CS75 Finance</h4>
<h4>Stock Information Quote</h4>
</div>
<div id="searchDiv" align="center">
<form id="searchForm">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>Symbol:</td>
<td><input type="text" id="search" size="10" /></td>
<td></td>
<td><input type="submit" value="Search" /></td>
</tr>
</table>
</form>
</div>
<div id="infoDiv" align="center">
<h4>Selected stock details:</h4>
Company: <span id="name"></span><br />
Price: $<span id="price"></span><br />
High: $<span id="high"></span><br />
Low: $<span id="low"></span><br />
</div>
<br />
<div id="buyDiv" align="center">
<form id="buyForm" method="post" action="buy.php">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td colspan="5" align="center"><b>Your current balance is: $</b><? echo number_format($userBalance,2); ?></td>
</tr>
<tr>
<td><input type="hidden" id="hSymbol" /></td>
<td align="right">Qty:</td>
<td><input type="text" id="qty" name="qty" size="10" value="1" /></td>
<td></td>
<td><input type="submit" value="Purchase" /></td>
</tr>
</table>
</form>
</div>
<?
$dsn = NULL;
?>
<br />
<hr width="70%">
<p align="center">CS75 Finance. Copyright 2012. All right reserved.</p>
</body>
</html>
Hope I posted correctly. Thanks in advance.
希望我发布正确。提前致谢。
回答by Robin Drexler
JS hint says, you have some errors in your code. Try to fix them and check again :)
JS 提示说,您的代码中有一些错误。尝试修复它们并再次检查:)
Line one is your $(document).ready
第一行是你的 $(document).ready
If you like to test it yourself: http://www.jshint.com/
如果你喜欢自己测试:http: //www.jshint.com/
Errors: Line 13: }, Extra comma.
Line 20: }, Extra comma.
Line 45: success: function(data) { Expected '}' to match '{' from line 40 and instead saw 'success'.
Line 45: success: function(data) { Expected ')' and instead saw ':'.
Line 45: success: function(data) { Missing semicolon.
Line 45: success: function(data) { Missing name in function declaration.
Line 66: }, Extra comma.
Line 74: }, Extra comma.
Line 92: }); Expected '(end)' and instead saw '}'.
错误:第 13 行:},多余的逗号。
第 20 行:},额外的逗号。
第 45 行:success: function(data) { Expected '}' to match '{' from line 40 and see 'success'.
第 45 行:success: function(data) { 预期为 ')' 而看到的是 ':'。
第 45 行:成功:函数(数据){ 缺少分号。
第 45 行:成功:函数(数据){ 函数声明中缺少名称。
第 66 行:},额外的逗号。
第 74 行:},额外的逗号。
第 92 行:}); 预期为 '(end)' 而看到的是 '}'。
回答by epoch
Your problem is caused by CDATA
not being correctly defined, it needs to have a //
before the start and end of it's declaration.
您的问题是由CDATA
未正确定义引起的,它需要//
在声明的开始和结束之前有一个。
<script type="text/javascript">
//<![CDATA[
// your code here....
//]]>
</script>
In addition to that, you have a few extra commas and some missing commas
除此之外,您还有一些额外的逗号和一些缺少的逗号