我正在开发一个用Python / Django编写的Web应用程序。特别是,我一直在研究一个日期'日期'用户在表单上输入的值未被保留(即,如果您刷新页面,日期字段将返回到用户更改之前保留的值。)
我已在此页面的HTML文件中添加了一个JS函数,以侦听表单上此值的更改,并在检测到更改时保存新值。
我添加的功能有效 - 如果我输入新值并刷新页面,我输入的值默认显示在字段中,而不是它保存的原始值。
但是,当我在浏览器中刷新页面时,控制台显示一条错误消息:
(index):3876 Uncaught SyntaxError:意外的令牌)
我对我的应用程序所做的唯一更改是将以下函数添加到.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
}
}
}
正如我所提到的,该功能正如我所希望的那样 - 当我在更改' date'的值后刷新页面。现在,它现在保留了用户设置的新值。
但是,出于某种原因,我收到了上面提到的未被捕获的语法错误。
我已多次重读此功能,但无法发现有问题的)
。
由于页面加载,并且所有函数现在都能正常工作,我无法想到为什么我会出现这种语法错误 - 如果确实存在语法错误,页面要么不会显示正确,或其中一个函数调用会破坏它?
控制台是否可能错误地显示此内容?我真的不想将我的更改提交到实时版本,直到我解决了这个问题,以防它确实破坏了某些内容,即使我现在看不到任何不能正常工作的内容。
如果我从我的HTML文件中删除上述功能并刷新页面,则控制台不再显示此语法错误,但我无法在该功能的任何位置发现我有额外的)
,或者有一个在错误的地方......
任何人都可以帮助我吗?我需要以某种方式刷新我的控制台吗?我正在使用Chrome查看网页。
答案 0 :(得分:0)
on
函数的第3个参数中存在语法错误。它应该是一个函数参考:
$(document).on('change', '#id_date_received', function messageDepositPaid()){
// ^ here you close the on
你可能想要这样的东西:
$(document).on('change', '#id_date_received', function() {
...
...
...
});