[英]JavaScript- Uncaught SyntaxError: Unexpected token ) - can't see the extra ')'
I am working on a web application that has been written in Python/ Django. 我正在使用Python / Django编写的Web应用程序。 In particular, I have been working on a bug where a 'date' value entered by the user on a form was not being retained (ie if you refreshed the page, the date field would return to the value it held prior to the user changing it).
特别是,我一直在研究一个错误,该错误未保留用户在表单上输入的“日期”值(即,如果刷新页面,则日期字段将返回到用户更改之前保留的值)它)。
I have added a JS function to the HTML file for this page to listen out for changes to this value on the form, and to save the new value any time a change is detected. 我已为该页面的HTML文件添加了一个JS函数,以侦听表单上对此值的更改,并在检测到任何更改时保存新值。
The function that I have added works- if I enter a new value, and refresh the page, the value that I entered is displayed in the field by default, rather than the original value that it held. 我添加的功能有效-如果输入新值并刷新页面,则默认情况下,该字段中会显示输入的值,而不是显示的原始值。
However, when I refresh the page in my browser, the console is showing an error message which says: 但是,当我在浏览器中刷新页面时,控制台显示一条错误消息,内容为:
(index):3876 Uncaught SyntaxError: Unexpected token )
(索引):3876未捕获的SyntaxError:意外的令牌)
The only change I've made to my application is adding the following function to the .html
file: 我对应用程序所做的唯一更改是将以下函数添加到
.html
文件中:
$(document).on('change', '#id_date_received', function messageDepositPaid()){
console.log("document.on('change') called on id_date_received in concept.html ")
if (window.confirm("Would you like to send an email confirming we have received a deposit?")) {
console.log("'if(window.confirm)' statement entered in concept.html JS... ")
var date = $(this).val()
if (date){
date = date.split('/')
var new_date = [];
new_date = new_date.concat(date[2]).concat(date[1]).concat(date[0]);
new_date = new_date.join('-');
}
if (new_date != $(this).data('original-value')){
// CDI fee date has been changed from nothing to something
console.log("It's changed")
// Set the original-value so it won't send multiple times
$(this).data('original-value', new_date) //ERF(24/11/2016 @ 1700) This line should be copying the value of new_date to the 'original-value', so original value should now hold the new date...
//$(this).data('original-value') = new_date
// Send email to relevant people
var url="{% url 'comms:open_email_template' project.id %}?template=5"
console.log('Url', url)
$.post(url)
.done(function(response){
console.log('Email sent')
})
// ERF(28/11/2016 @ 1400) Set value of 'date' to the value of 'new_date':
date = new_date
}
}
/*} */
else{
console.log("'else' of 'if(window.confirm)' statement entered in concept.html JS... ")
var date = $(this).val()
if (date){
date = date.split('/')
var new_date = [];
new_date = new_date.concat(date[2]).concat(date[1]).concat(date[0]);
new_date = new_date.join('-');
console.log("Value of date in if(date): ", date)
}
if (new_date != $(this).data('original-value')){
//console.log("Value of this.project.date_received: ", this.project.date_received)
console.log("Value of date: ", date)
// CDI fee date has been changed from nothing to something
/*ERF(28/11/2016 @ 0935) Fee date was not necessarily nothing- need to change its value when it had a date
previously stored in it too...
Print the values of 'new_date' and 'original-value' here. */
console.log("Value of new_date: ", new_date)
console.log("Value of original-value: ", this.data)
console.log("It's changed")
// Set the original-value so it won't send multiple times
$(this).data('original-value', new_date) /*ERF(28/11/2016 @ 0950) This line isn't actually setting the original value to the value of 'new_date'...
Set the value of 'id_date_received' to the 'new_date'
Need to set the date_received attribute of the project/ presentation object deposit to this too. */
id_date_received = new_date
console.log("Value of id_date_received: ", id_date_received)
date_received = new_date
console.log("value of date_received: ", date_received)
// Send email to relevant people
var url="{% url 'comms:open_email_template' project.id %}?template=5"
console.log('Url', url)
$.post(url)
.done(function(response){
console.log('Email sent')
})
// ERF(28/11/2016 @ 1400) Set value of 'date' to the value of 'new_date':
date = new_date
}
}
}
As I mentioned, the function works as I had hoped- and when I refresh the page after changing the value of the 'date' field, it now retains the new value that the user has set. 正如我所提到的,该函数按预期运行,并且在更改“日期”字段的值后刷新页面时,该函数现在保留用户设置的新值。
However, for some reason, I'm getting the uncaught syntax error that I mentioned above. 但是,由于某种原因,我遇到了上面提到的未捕获的语法错误。
I've re-read through this function several times, but can't spot the offending )
. 我已经多次阅读了此函数,但无法发现问题
)
。
Since the page loads, and all of the functions now work correctly, I can't think why I would be getting this syntax error- surely if there actually was a syntax error, the page either wouldn't display correctly, or one of the function calls would break it? 由于页面已加载,并且所有功能现在都可以正常运行,所以我不认为为什么会出现此语法错误-如果确实存在语法错误,或者页面无法正确显示,或者页面之一函数调用会中断吗?
Is it possible that the console is incorrectly displaying this? 控制台是否可能会错误地显示此内容? I don't really want to commit my changes to the live version until I resolve this in case it does actually break something, even though I can't see anything that's not working at the moment...
我真的不希望将更改提交到实时版本,直到我解决此问题,以防它确实破坏了某些内容,即使我目前看不到任何不起作用的内容...
If I delete the above function from my HTML file, and refresh the page, the console no longer displays this syntax error, but I can't spot anywhere in that function where I have an extra )
, or have one in the wrong place... 如果我删除我的HTML文件,上述功能,并刷新页面,控制台不再显示此语法错误,但我不能在函数的任何位置发现在那里我有一个额外的
)
,或者有一个在错误的地方。 ..
Can anyone help me out here? 有人可以帮我从这里出去吗? Do I need to refresh my console somehow?
我是否需要以某种方式刷新控制台? I am using Chrome to view the webpage.
我正在使用Chrome浏览网页。
You have a syntax error in the 3rd parameter of the on
function. 您在
on
函数的第3个参数中存在语法错误。 It should be a function reference: 它应该是一个函数参考:
$(document).on('change', '#id_date_received', function messageDepositPaid()){
// ^ here you close the on
You probably want something like this: 您可能想要这样的东西:
$(document).on('change', '#id_date_received', function() {
...
...
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.