简体   繁体   English

jQuery在单击通用类时获取元素的ID

[英]Jquery get id of an element on click of a common class

I am programmatically populating options to increase and decrease the value of an element and store the same in the DB table. 我正在以编程方式填充用于增加和减少元素值并将其存储在DB表中的选项。 To get a better idea, consider the following example: 为了获得更好的主意,请考虑以下示例:

<tr>
  <td id="name_1">Element 1</td>
  <td><a href="#" class="increase" id="inc_1">increase icon</a></td>
  <td><a href="#" class="decrease" id="dec_1">decrease icon</a></td>
</tr>

<tr>
  <td id="name_2">Element 2</td>
  <td><a href="#" class="increase" id="inc_2">increase icon</a></td>
  <td><a href="#" class="decrease" id="dec_2">decrease icon</a></td>
</tr>

<tr>
  <td id="name_n">Element n</td>
  <td><a href="#" class="increase" id="inc_n">increase icon</a></td>
  <td><a href="#" class="decrease" id="dec_n">decrease icon</a></td>
</tr>

Whenever I click any among the n increase / decrease icon, I need to access the value of #name_n . 每当我单击n增加/减少图标中的任何一个时,我都需要访问#name_n的值。 For which, I wrote the following function: 为此,我编写了以下函数:

$(".increase").click(function(){
  var id = $(this).attr('id'); //get the id of the element that was clicked.
  console.log(id);

  var arr = id.split("_"); //split to get the number
  var no = arr[1]; //get the number

  var name = $("#name_"+no).text(); //get the required value in name_n
  console.log(name); 
 });
//replica for decrease class as well.

Problem : Every time I click any increase icon, in my console , I'm getting id as inc_1 only! 问题:每次单击console 任何增加图标时,我只会获得id作为inc_1 So, the value of name is Element 1 always. 因此, name的值始终为Element 1 Same happens with click function for .decrease . click功能的.decrease发生同样的情况。

I have tried with the following ways to get the id : 我尝试了以下方法来获取id

var id = this.id;
var id = $(this).get(0).id;
var id = $(this)[0].id;

But nothing changed. 但是什么都没有改变。 The same problem persists. 同样的问题仍然存在。 What's wrong and how do I resolve this? 有什么问题,我该如何解决?

Your code looks correct at first glance. 乍一看,您的代码看起来正确。 Maybe there is some issue with rendering, perhaps the elements really get the same ID. 渲染可能存在一些问题,也许元素确实获得了相同的ID。

However, I would recommend a different approach for this task, without using ID's. 但是,对于不使用ID的情况,我建议针对此任务采用其他方法。 You can achieve your goal by referencing the TD element relatively to the clicked increase/decrease button. 您可以通过相对于单击的增加/减少按钮引用TD元素来实现目标。 I mean something like this. 我的意思是这样的。

$(".increase").click(function(){
  var $td = $(this).closest("tr").children(":eq(0)"); //get the TR's first TD

  var name = $td.text(); //get the required value in name_n td
  console.log(name); 
 });

You could add a more generic class on the elements you wish tou target after the click(currently #name_n) and use the .closest and .siblings methods. 您可以在click(当前为#name_n)之后希望定位的元素上添加一个更通用的类,并使用.closest.siblings方法。

html html

<tr>
  <td id="name_n" class="target">Element n</td>
  <td><a href="#" class="increase" id="inc_n">increase icon</a></td>
  <td><a href="#" class="decrease" id="dec_n">decrease icon</a></td>
</tr>

js js

$(".increase").click(function(){
    var name = $(this).closest('td').siblings('.target').text();
    console.log(name);
});

Here is a working demo https://jsfiddle.net/0hru2jtx/ 这是一个有效的演示https://jsfiddle.net/0hru2jtx/

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

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