How to get data-id attribute value in Jquery?

时间:2016-09-01 06:20:37

标签: javascript jquery

HTML CODE

<tbody>
    <tr>
        <td>0</td>
        <td>204093D-P12</td>
        <td>80443</td>
        <td>Name</td>
        <td><span class="label label-success">Updated</span></td>
        <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td>
    </tr><tr>
        <td>1</td>
        <td>216619D-P18</td>
        <td>16009</td>
        <td>Name</td>
        <td><span class="label label-success">Updated</span></td>
        <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td>
    </tr><tr>
        <td>2</td>
        <td>21663P0012</td>
        <td>13116</td>
        <td>Name</td>
        <td><span class="label label-success">Updated</span></td>
        <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td>
    </tr><tr>
        <td>3</td>
        <td>217496D-P078</td>
        <td>16032</td>
        <td>Name</td>
        <td><span class="label label-success">Updated</span></td>
        <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td>
    </tr>
</tbody>    

And i have to tried to get data-id attribute value from using Jquery in following way

function ShowModal(){
      alert($(this).attr("data-id"));
}

but return undefinedhow to get data-id value from jquery? and i have an another doubt data-id value can hold numeric value or string value?

6 个答案:

答案 0 :(得分:7)

You need to pass the current element context in inline click handler like

<button onClick="ShowModal(this)" data-id="217496D-P078"></button>

Then use the passed element reference to get the data-id. You can also use HTMLElement.dataset property like elem.dataset.id

function ShowModal(elem){
    var dataId = $(elem).data("id");
    alert(dataId);
}

Additionally, I would recommend you use to bind event handler's instead of ugly inline click handler.

答案 1 :(得分:3)

One of the important part of jquery is manipulate the DOM.

DOM = Document Object Model : Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

It has several DOM Manipulation :

  1. Get Content : text(), html(), and val()
  2. Get Attributes : attr()

Now if u have to access a particular attribute on click of button or something else then use this :

so here is my HTML

<p><a href="http://www.google.com" id="test" data-id="id_12345">google.com</a></p>

<button>Click Here</button>

then u have to access the attributes of anchor tag like this:

$("button").click(function(){
    alert($("#test").attr("href")); // output : http://www.google.com
    alert($("#test").attr("data-id")); // output : id_12345
}); 

答案 2 :(得分:1)

您可以使用Javascript的数据集或JQuqery data方法:

&#13;
&#13;
$(document).ready(function() {
  $('button').on('click', function() {
    console.log(this.dataset.id, $(this).data().id, $(this).data('id'));
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tbody>
  <tr>
    <td>
      <button type="button" data-id="1">btn #1</button>
      <button type="button" data-id="2">btn #2</button>
      <br>
    </td>
  </tr>
  <tr>
    <td>
      <button type="button" data-id="3">btn #1</button>
      <button type="button" data-id="4">btn #2</button>
      <br>
    </td>
  </tr>
  <tr>
    <td>
      <button type="button" data-id="5">btn #1</button>
      <button type="button" data-id="6">btn# 2</button>
      <br>
    </td>
  </tr>
  <tr>
    <td>
      <button type="button" data-id="7">btn #1</button>
      <button type="button" data-id="8">btn #2</button>
      <br>
    </td>
  </tr>
</tbody>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

 $(function () {
            $(".inputs").click(function (e) {
                alert($(this).attr("data-id"));
            });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="btn btn-xs btn-flat inputs" value="click"   type="button" data-toggle="modal" data-id="217496D-P078" data-target="#myModal"/>

答案 4 :(得分:0)

您的代码是正确的,所有需要做的就是将其传递给您的函数,如:

<button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal" type="button" title="Add" onClick="ShowModal(this)"><i class="fa fa-plus" aria-hidden="true"></i></button>

答案 5 :(得分:0)

You have to create unique data-id in your code data-id not unique.


data-id="217496D-P078" data-target="#myModal" type="button" title="Add" 
data-id="217496D-P078" data-target="#myModal_edit" type="button" title="
data-id="217496D-P078" data-target="#myModal_details" type="button" titl

then

Try this

<button onClick="ShowModal(this)" data-id="217496D-P078"></button>


function ShowModal(elem){
    var dataId = $(elem).data("id");
    alert(dataId);
}