简体   繁体   English

Jquery.map 特定元素的点击事件

[英]Jquery .map Click Event on specific Element

Helllo, i have a Array like this:你好,我有一个这样的数组:

let Array1 = [
    {name:"try1", id:"id1", symbol:"symbol1"},
    {name:"try2", id:"id2", symbol:"symbol2"},
    {name:"try3", id:"id3", symbol:"symbol3"},
    {name:"try4", id:"id4", symbol:"symbol4"},
    {name:"try5", id:"id5", symbol:"symbol5"}
]

and i want to make each of them a button.我想让他们每个人都成为一个按钮。 that i can click on each one.我可以点击每一个。 for this, i use.map Funnction ( im not sure this is the best option )为此,我使用 map 函数(我不确定这是最好的选择)

Array1.map(data => $(".check").html(`<a id="${data.id}" class="btn btn-info" href="#" role="button">${data.id}</a>`))

until now i get 5 buttons.直到现在我得到 5 个按钮。 1 button each object with the id of the button. 1 个按钮,每个 object 带有按钮的 ID。

the problem start here: i trying to catch a Click Event on this buttons.问题从这里开始:我试图在这个按钮上捕捉一个点击事件。 i cant make it done..我做不到..

I tried:我试过:

(`${data.id}`).click(e => console.log("click))

I would be happy for help or any other way with the same result.我很乐意寻求帮助或以任何其他方式获得相同的结果。 Thankss !!谢谢!

You can simply use forEach function with event Delegation for your dynamically added buttons .您可以简单地将forEach function 与事件委托一起用于动态添加的buttons

Also you need use jQuery .append() function which will add all the button in the .check class. Using .html will replace the last one only shows the last button.您还需要使用 jQuery .append() function 这将在.check class 中添加所有按钮。使用.html将替换最后一个只显示最后一个按钮。

Run snippet below.运行下面的代码片段。 Clicks are working on each one. Clicks对每一个都有效。

 let Array1 = [{ name: "try1", id: "id1", symbol: "symbol1" }, { name: "try2", id: "id2", symbol: "symbol2" }, { name: "try3", id: "id3", symbol: "symbol3" }, { name: "try4", id: "id4", symbol: "symbol4" }, { name: "try5", id: "id5", symbol: "symbol5" } ] //Loop through the array1 Array1.forEach(function(data) { //Append Each button $(".check").append(`<a id="${data.id}" class="btn btn-info myBtn" href="#" role="button">${data.id}</a><br>`) }) //Click function on buttons $(document).on('click', '.myBtn', function() { //get the id let getId = $(this).attr('id') //do something //Console console.log(getId) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="check"> </div>

You can use a passive listener:您可以使用被动侦听器:

 let Array1 = [ {name:"try1", id:"id1", symbol:"symbol1"}, {name:"try2", id:"id2", symbol:"symbol2"}, {name:"try3", id:"id3", symbol:"symbol3"}, {name:"try4", id:"id4", symbol:"symbol4"}, {name:"try5", id:"id5", symbol:"symbol5"} ] Array1.map(data => $(".check").append(`<a id="${data.id}" class="btn btn-info new-btn" href="#" role="button">${data.id}</a><br>`)) $(document).on("click", ".new-btn", (e) => console.log(e.target.id))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="check"></div>

 let Array1 = [{ name: "try1", id: "id1", symbol: "symbol1" }, { name: "try2", id: "id2", symbol: "symbol2" }, { name: "try3", id: "id3", symbol: "symbol3" }, { name: "try4", id: "id4", symbol: "symbol4" }, { name: "try5", id: "id5", symbol: "symbol5" } ]; Array1.forEach(({ id }) => { $(".check").append(`<a id="${id}" class="btn btn-info" href="#" role="button">${id}</a>`); $('#' + id).on('click', function(e) { e.preventDefault(); console.log($('#' + id).text()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="check"></div>

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

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