简体   繁体   English

动态元素从最后点击的元素中获取数据

[英]Dynamic elements are taking up data from the last clicked element

I am trying to add ajax-fetched data into elements which are dynamically added to the DOM with jquery. 我试图将ajax提取的数据添加到使用jquery动态添加到DOM的元素中。 The elements have the same class but different IDs. 元素具有相同的类,但ID不同。 They are added by 'click' events. 它们是通过“点击”事件添加的。 Everything works fine. 一切正常。 The problem is all the appended elements display data fetched by the ID of the last clicked item. 问题在于所有附加元素都显示通过最后单击的项目的ID提取的数据。 I'm not sure how I can demonstrate this in jsfiddle, but here is an idea of what I am talking about HTML: 我不确定如何在jsfiddle中进行演示,但这是我在谈论HTML的一个思路:

<li class="options" id="1">Item 1</li>
<li class="options" id="2">Item 2</li>
<li class="options" id="3">Item 3</li>
<br/>
<div class="boxes"></div>

JS: JS:

//how to add the box to the DOM
$('li.options').click(function() {
  id = $(this).attr('id');
  $('.boxes').append('<div class="box" id="'+id+'"><div class="box-content"></div></div>');
  //Fetching data via ajax
  $.ajax({
    type: 'POST',
    url: '/echo/json/',
    dataType: 'json',
    cache: false,
    data: {
      'id': id
    },
    success: function(res) {
      $(this).find('box-content').append(res);
    }
  });
});

Here is the example in jsfiddle 这是jsfiddle中的示例

https://jsfiddle.net/pkgf7enj/11/ https://jsfiddle.net/pkgf7enj/11/

Assume that the data received is equal to the ID. 假设接收到的数据等于ID。 ie, if item 1 is clicked, the returned data (via ajax) is 1, if item 2 clicked, 2 is returned etc. 即,如果单击了项目1,则返回的数据(通过ajax)为1,如果单击了项目2,则返回2,依此类推。

Now, if I click on item 1, box with '1' is appended. 现在,如果我单击项目1,将附加带有“ 1”的框。 If I click on item 2, box 2 is appended, but both box 1 and 2 will have value 2 (last clicked item). 如果我单击项目2,则将附加框2,但框1和2都将具有值2(最后单击的项目)。 If I then click on 3, the 3 boxes will have 3. 如果我再单击3,则3个框将有3个。

How can I solve this? 我该如何解决? Someone please 有人请

You can see my changes in my comments 您可以在评论中看到我的更改

 //how to add the box to the DOM $('li.options').click(function() { //insert var before id var id = $(this).attr('id'); //To be sure that it will display the good "id", i add the id value in the box content. You can remove it in your own code $('.boxes').append('<div class="box" id="'+id+'">'+id+'<div class="box-content"></div></div>'); //Fetching data via ajax $.ajax({ type: 'POST', url: '/echo/json/', dataType: 'json', cache: false, data: { 'id': id }, success: function(res) { $(this).find('box-content').append(res); } }); }); 
 li { cursor: pointer; } .box { background: red; height: 50px; width: 50px; } .box-content { width: 100%; height: 100%; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li class="options" id="1">Item 1</li> <li class="options" id="2">Item 2</li> <li class="options" id="3">Item 3</li> <br/> <div class="boxes"></div> 

When I run your fiddle, with one minor addition, it posts the correct id in each box. 当我运行您的小提琴时,加上一点点小提琴,它就会在每个框中张贴正确的ID。 However you have a bug in your fiddle code. 但是,您的小提琴代码中存在错误。 Your code reads: 您的代码显示为:

 $('.boxes').append('<div class="box" id="'+id+'"><div class="box-content"></div></div>');

I updated it to display the id in each red box: 我更新了它以在每个红色框中显示id:

 $('.boxes').append('<div class="box" id="'+id+'">'+id+'<div class="box-content"></div></div>');

The bug in your code is that you are naming each target box with the same ID as the sourcing line item, which results in multiple dom objects with the same id. 代码中的错误是,您要为每个目标框命名与采购订单项具有相同的ID,这会导致多个dom对象具有相同的ID。 This can give you highly irregular results when you are retrieving by id. 按ID检索时,这可能会给您带来非常不规则的结果。 If you update your code to the following, then the first time you select each line item in your fiddle, the targeted boxes will have a unique id. 如果将代码更新为以下代码,则在第一次选择小提琴中的每个订单项时,目标框将具有唯一的ID。 If you are going to allow each line item to be clicked more than once, then you'll want to add an incremented singleton variable to keep the box id's all unique. 如果要允许每个订单项被多次单击,则需要添加一个递增的单例变量,以使框ID保持唯一。

unique the first iteration 唯一的第一次迭代

 $('.boxes').append('<div class="box" id="box'+id+'">'+id+'<div class="box-content"></div></div>');

unique every iteration: 每次迭代都是唯一的:

var _s_iter = 0;
$('li.options').click(function() {
  id = $(this).attr('id');
  console.log(id)
  $('.boxes').append('<div class="box" id="box_'+_s_iter+'_'+id+'">'+id+'<div class="box-content"></div></div>');
_s_iter++;
  //Fetching data via ajax

Hope this helps 希望这可以帮助

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

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