简体   繁体   English

未捕获的语法错误:标识符已被声明

[英]Uncaught SyntaxError: Identifier has already been declared

I want to make a modal-cookie thing where on page load, the modal checks if there is a cookie, and then if there is no "name" cookie, the modal will show.我想在页面加载时制作一个模态cookie,模态检查是否有cookie,然后如果没有“名称”cookie,模态将显示。 otherwise, the modal won't show.否则,模态不会显示。 However, I have this error:但是,我有这个错误:

Uncaught SyntaxError: Identifier 'addCookie' has already been declared.未捕获的 SyntaxError: 标识符“addCookie”已被声明。

Because of this error, my cookies do not save.由于这个错误,我的 cookie 没有保存。

I understand that this error happens when someone is trying to override a let .我知道当有人试图覆盖let时会发生此错误。 However, I didn't try to override it, I just tried to call it但是,我没有尝试覆盖它,我只是尝试调用它

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

<div id="modalWelcome" class="modal fade" data-backdrop="false">
  <div class="form-group">
    <input type="text" class="form-control" placeholder="your name" id="fname">
  </div>
  <button type="submit" class="btn btn-primary" onclick="addCookie()">go!</button>
</div>
if ($.cookie('name') == undefined) {
  $("#modalWelcome").modal('show');
} else {
  $('#modalWelcome').modal('hide');
}

var cookiename;
let addCookie = () => {
  cookiename = $('#fname').val();
  $.cookie("name", cookiename);
}
  1. Change onclick="addCookie()" to onclick="addCookie" because you need to set the handler, not execute.onclick="addCookie()"更改为onclick="addCookie"因为您需要设置处理程序,而不是执行。
  2. Check how your script is included because you could include it twice.检查您的脚本是如何包含的,因为您可以将它包含两次。

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

相关问题 未捕获的 SyntaxError:标识符 '$...' 已被声明 - Uncaught SyntaxError: Identifier '$…' has already been declared javascript-未捕获的语法错误:标识符 * 已被声明 - javascript- Uncaught SyntaxError: Identifier * has already been declared 未捕获的SyntaxError:标识符&#39;baseUrl&#39;已被声明 - Uncaught SyntaxError: Identifier 'baseUrl' has already been declared 未捕获的 SyntaxError:标识符“按钮”已被声明 - Uncaught SyntaxError: Identifier 'Buttons' has already been declared 未捕获的语法错误:标识符“总计”已被声明 - Uncaught SyntaxError: Identifier 'total' has already been declared 未捕获的语法错误:标识符“myOptions”已在 JavaScript 中声明 - Uncaught SyntaxError: Identifier 'myOptions' has already been declared in JavaScript 未捕获的语法错误:标识符“x”已被声明 - Uncaught SyntaxError: Identifier 'x' has already been declared 如何修复未捕获的语法错误:标识符“翻译”已经声明 - how to fix Uncaught SyntaxError: Identifier 'translate' has already been declared 未捕获的SyntaxError:标识符&#39;o&#39;已经被声明 - Uncaught SyntaxError: Identifier 'o' has already been declared Uncaught SyntaxError: Identifier '[x]' has been declared - 为什么? - Uncaught SyntaxError: Identifier '[x]' has already been declared - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM