繁体   English   中英

JavaScript-未捕获的SyntaxError:意外令牌)-无法看到多余的')'

[英]JavaScript- Uncaught SyntaxError: Unexpected token ) - can't see the extra ')'

我正在使用Python / Django编写的Web应用程序。 特别是,我一直在研究一个错误,该错误未保留用户在表单上输入的“日期”值(即,如果刷新页面,则日期字段将返回到用户更改之前保留的值)它)。

我已为该页面的HTML文件添加了一个JS函数,以侦听表单上对此值的更改,并在检测到任何更改时保存新值。

我添加的功能有效-如果输入新值并刷新页面,则默认情况下,该字段中会显示输入的值,而不是显示的原始值。

但是,当我在浏览器中刷新页面时,控制台显示一条错误消息,内容为:

(索引):3876未捕获的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
        }
    }
}

正如我所提到的,该函数按预期运行,并且在更改“日期”字段的值后刷新页面时,该函数现在保留用户设置的新值。

但是,由于某种原因,我遇到了上面提到的未捕获的语法错误。

我已经多次阅读了此函数,但无法发现问题)

由于页面已加载,并且所有功能现在都可以正常运行,所以我不认为为什么会出现此语法错误-如果确实存在语法错误,或者页面无法正确显示,或者页面之一函数调用会中断吗?

控制台是否可能会错误地显示此内容? 我真的不希望将更改提交到实时版本,直到我解决此问题,以防它确实破坏了某些内容,即使我目前看不到任何不起作用的内容...

如果我删除我的HTML文件,上述功能,并刷新页面,控制台不再显示此语法错误,但我不能在函数的任何位置发现在那里我有一个额外的) ,或者有一个在错误的地方。 ..

有人可以帮我从这里出去吗? 我是否需要以某种方式刷新控制台? 我正在使用Chrome浏览网页。

您在on函数的第3个参数中存在语法错误。 它应该是一个函数参考:

$(document).on('change', '#id_date_received', function messageDepositPaid()){
//                                                                         ^ here you close the on

您可能想要这样的东西:

$(document).on('change', '#id_date_received', function() {
    ...
    ...
    ...
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM