简体   繁体   English

使用 JSON 关联数组向多个元素添加事件侦听器

[英]Using JSON Associative Array to Add Event Listeners to Multiple Elements

I am using JavaScript to iterate through a JSON associative array to add an event listener to a collection of different forms on the same page.我正在使用 JavaScript 遍历 JSON 关联数组,以将事件侦听器添加到同一页面上不同 forms 的集合中。 The key of the array is the ID of the object on the page and the value is the message to appear in a confirmation box when the form is submitted.数组的键是页面上 object 的 ID,值是提交表单时出现在确认框中的消息。

For example,I have a series of forms with IDs: confirm_1 , confirm_2 , confirm 2 .例如,我有一系列的 forms,ID 为: confirm_1confirm_2confirm 2

On the same page I have something like this:在同一页面上,我有这样的事情:

<script type="application/json" id="json_data">{"confirm_1": "Are you sure?", "confirm_2": "Really?", "confirm_3": "Must you?"}</script>
<script src="confirmation.js"></script>

And this is confirmation.js :这是confirmation.js

var data = JSON.parse(document.getElementById('json_data').innerHTML);

for (var key in data) {
   document.getElementById(key).addEventListener('submit', function(event) {
      if (!confirm(data[key])) {
         event.preventDefault();
      }
   });
};

The problem is that when I click the submit button on any form they are all assigned the confirmation message of the final element in the array.问题是,当我单击任何表单上的提交按钮时,它们都被分配了数组中最后一个元素的确认消息。 It's like all the previous addEventListener calls inherit the last call made.就像所有以前的 addEventListener 调用都继承了最后一次调用。

What am I missing here?我在这里想念什么?

You are facing issue because you have declared key as var datatype.您正面临问题,因为您已将key声明为var数据类型。 Since it has function scope, it gets updated with the last value assigned to it which is confirm_3 in your case.由于它具有 function scope,因此它会使用分配给它的最后一个值进行更新,在您的情况下是confirm_3 This problem can be solved in different ways.这个问题可以通过不同的方式来解决。 I will now mention three ways to solve it.我现在将提到解决它的三种方法。

  1. If you are okay with using ES6, you could just use let instead of var to declare key variable.如果你对使用 ES6 没问题,你可以使用let而不是var来声明key变量。 Since let has block scope, each loop block will have its own space for key variable.由于let有块 scope,每个循环块将有自己的空间用于key变量。
  2. By binding the value to the callback function.通过将值绑定到回调 function。
  3. By using Immediate Invoke function.通过使用立即调用 function。

I have addressed all these in the example below and have commented them for readability.我已经在下面的示例中解决了所有这些问题,并为便于阅读对它们进行了评论。

 var data = { confirm_1: "Are you sure?", confirm_2: "Really?", confirm_3: "Must you?" }; //Using let /*for (let key in data) { document.getElementById(key).addEventListener("click", function (event) { alert(data[key]); }); }*/ //Using Bind for (var key in data) { document.getElementById(key).addEventListener( "click", function (message, event) { alert(message); }.bind(this, data[key]) ); } //Using IIF (Immediate Invoking function) /* for (var key in data) { (function(message){ document.getElementById(key).addEventListener("click", function (event) { alert(message); }); })(data[key]); } */
 <button id='confirm_1'>confirm_1</button> <button id='confirm_2'>confirm_2</button> <button id='confirm_3'>confirm_3</button>

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

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