简体   繁体   English

如何在JavaScript中修复Uncaught TypeError?

[英]How to fix Uncaught TypeError in JavaScript?

I'm trying to submit a form of a specific element in my webpage but i get a 我正在尝试在我的网页中提交一个特定元素的表单,但我得到了一个

Uncaught TypeError: Cannot read property 'form' of undefined

This is the JavaScript script for my webpage 这是我的网页的JavaScript脚本

<script type="text/javascript">
              var aTags = document.getElementsByName("s");
    for (var i=0;i<aTags.length;i++){
        aTags[i].addEventListener('click', function(e){
          e.preventDefault();
          bootbox.confirm({
    message: "This is a confirm with custom button text and color! Do you like it?",
    closeButton: false,
    buttons: {
        confirm: {
            label: 'Yes',
            className: 'btn-success'
        },
        cancel: {
            label: 'No',
            className: 'btn-danger'
        }
    },
    callback: function (result) {
      if(result){
        aTags[i].form.submit();
      }
    }
});
        });
            }
              </script>

Every one of the click handlers is referencing the exact same i variable. 每个点击处理程序都引用完全相同的i变量。 By the time the button is clicked, i has long ago been incremented to aTags.length . 单击按钮时, i很久以前已经增加到aTags.length So aTags[i] resolves to aTags[aTags.length] , which is undefined. 所以aTags[i]解析为aTags[aTags.length] ,这是未定义的。

Simplest solution is to just use let instead of var . 最简单的解决方案是使用let而不是var That way, every time through the loop gets a new binding of the variable, and thus the click handlers are all associated with the correct value. 这样,每次循环都会获得变量的新绑定,因此单击处理程序都与正确的值相关联。

for (let i = 0; i < aTags.length; i++){
   // rest of the code the same
}

暂无
暂无

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

相关问题 如何修复未捕获的TypeError - How to fix an Uncaught TypeError 如何在JavaScript中修复“未捕获的TypeError:action.addEventListener不是函数” - How to fix “Uncaught TypeError: action.addEventListener is not a function” in JavaScript 如何修复“script.js:5 Uncaught TypeError: ...”JavaScript 错误 - How to fix "script.js:5 Uncaught TypeError: ..." JavaScript error 如何修复 Uncaught TypeError: mapster is not a function? - How to fix Uncaught TypeError: mapster is not a function? javascript - Uncaught (in promise) TypeError: e.iterator is not a function。 如何修复此类错误? - javascript - Uncaught (in promise) TypeError: e.iterator is not a function. How to fix such error? 在同一页面上两次启动自定义JavaScript插件时,如何解决“未捕获的TypeError:tagsInput不是构造函数”错误 - How to fix 'Uncaught TypeError: tagsInput is not a constructor' error while initiating custom JavaScript plugin two times on the same page 不知道如何解决这个问题:“Uncaught TypeError (webcam.snap is not a function)” &lt;— javascript - Can't figure out how to fix this: “Uncaught TypeError (webcam.snap is not a function)” <— javascript 如何修复未捕获的类型错误:无法在 wordpress 的 javascript 中设置 null 的属性“innerHTML”? - How to fix Uncaught TypeError: Cannot set property 'innerHTML' of null in javascript in wordpress? 我该如何解决:未捕获的TypeError:无法读取JavaScript中未定义的属性&#39;toString&#39; - how can i fix this: Uncaught TypeError: Cannot read property 'toString' of undefined in javascript 如何解决JavaScript中的“未捕获的TypeError:未定义不是函数”? - How to resolve “Uncaught TypeError: undefined is not a function” in javascript"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM