【问题标题】:Getting "Uncaught SyntaxError: Invalid or unexpected token" in JavaScript在 JavaScript 中获取“未捕获的 SyntaxError:无效或意外令牌”
【发布时间】:2019-04-20 09:11:08
【问题描述】:

我在使用预订表格时在我的网站上收到此错误。

未捕获的 SyntaxError:无效或意外令牌 在 Function.Date.createNewFormat (jquery.stmdatetimepicker.js?ver=4.2.7:1857) 在 Date.dateFormat (jquery.stmdatetimepicker.js?ver=4.2.7:1857) 在 XDSoft_datetime._this.str (jquery.stmdatetimepicker.js?ver=4.2.7:1196) 在 HTMLDivElement。 (jquery.stmdatetimepicker.js?ver=4.2.7:1549) 在 HTMLDivElement.dispatch (jquery.js?ver=1.12.4:3) 在 HTMLDivElement.r.handle (jquery.js?ver=1.12.4:3)

这是链接:mantovacar.com/wp-content/themes/motors/assets/js/jquery.stmdatetimepicker.js?ver=4.2.7

【问题讨论】:

  • 请生成minimal reproducible example,以便在您的网站修复后仍然可以理解问题。您可以在编辑器中使用 Stack Snippet,以便在问题本身中重现问题。
  • 我只是不知道该怎么做,先生,您能帮帮我吗?
  • 目前不太可能有人可以帮助您(除非他们为您完成所有工作)。你需要帮助别人帮助你。
  • 你说得对,谢谢

标签: javascript jquery arrays datetime datepicker


【解决方案1】:

我已经调试过了,这是你的问题: var dateTimeFormat = 'j F Y G \h i \m\i\n';

您可能希望至少将\n 替换为n,但整个格式对我来说似乎无效。

之所以出现问题,是因为插件从格式创建代码作为字符串,然后在其上调用eval(),这显然是一种不好的做法。当您将\n 作为格式的一部分传递时,它会转换为(换行符)。它会破坏代码,因为它在字符串中,如下所示:

Date.prototype.format2 = function() { ... + '
';}

我建议你使用这个库的更新版本(也许他们已经修复了它):https://xdsoft.net/jqplugins/datetimepicker/

并考虑切换到一些更常见的日期格式,例如Y/m/d H:i

我找不到有关您的插件格式的文档,因此您至少可以检查源以查看您可以在 dateTimeFormat 中使用的所有可用字母:

Date.getFormatCode = function(a) {
    switch (a) {
    case "d":
        return "String.leftPad(this.getDate(), 2, '0') + ";
    case "D":
        return "Date.dayNames[this.getDay()].substring(0, 3) + ";
    case "j":
        return "this.getDate() + ";
    case "l":
        return "Date.dayNames[this.getDay()] + ";
    case "S":
        return "this.getSuffix() + ";
    case "w":
        return "this.getDay() + ";
    case "z":
        return "this.getDayOfYear() + ";
    case "W":
        return "this.getWeekOfYear() + ";
    case "F":
        return "Date.monthNames[this.getMonth()] + ";
    case "m":
        return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
    case "M":
        return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
    case "n":
        return "(this.getMonth() + 1) + ";
    case "t":
        return "this.getDaysInMonth() + ";
    case "L":
        return "(this.isLeapYear() ? 1 : 0) + ";
    case "Y":
        return "this.getFullYear() + ";
    case "y":
        return "('' + this.getFullYear()).substring(2, 4) + ";
    case "a":
        return "(this.getHours() < 12 ? 'am' : 'pm') + ";
    case "A":
        return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
    case "g":
        return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
    case "G":
        return "this.getHours() + ";
    case "h":
        return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
    case "H":
        return "String.leftPad(this.getHours(), 2, '0') + ";
    case "i":
        return "String.leftPad(this.getMinutes(), 2, '0') + ";
    case "s":
        return "String.leftPad(this.getSeconds(), 2, '0') + ";
    case "O":
        return "this.getGMTOffset() + ";
    case "T":
        return "this.getTimezone() + ";
    case "Z":
        return "(this.getTimezoneOffset() * -60) + ";
    default:
        return "'" + String.escape(a) + "' + ";
    }
}

另外,您的 js 文件编码似乎有一些问题,您应该检查一下并切换到 UTF-8,可能。但这超出了问题范围。

现在,只需更改索引页面上的 dateTimeFormat 变量即可。

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 2018-08-25
    • 1970-01-01
    • 2020-10-13
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多