HTML CODE
HTML代码
<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
我必须尝试以下列方式使用Jquery获取data-id属性值
function ShowModal(){
alert($(this).attr("data-id"));
}
but return undefined
how to get data-id value from jquery? and i have an another doubt data-id value can hold numeric value or string value?
但返回undefinedhow从jquery获取data-id值?我有另一个疑问data-id值可以保存数值或字符串值?
6 个解决方案
#1
8
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
然后使用传递的元素引用来获取data-id。您还可以使用像elem.dataset.id这样的HTMLElement.dataset属性
function ShowModal(elem){
var dataId = $(elem).data("id");
alert(dataId);
}
Additionally, I would recommend you use jquery to bind event handler's instead of ugly inline click handler.
另外,我建议你使用jquery绑定事件处理程序而不是丑陋的内联单击处理程序。
#2
3
One of the important part of jquery is manipulate the DOM.
jquery的一个重要部分是操纵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.
DOM =文档对象模型:文档对象模型(DOM)是一个平台和语言中立的接口,允许程序和脚本动态访问和更新文档的内容,结构和样式。
It has several DOM Manipulation :
它有几个DOM操作:
- Get Content : text(), html(), and val()
- 获取内容:text(),html()和val()
- Get Attributes : attr()
- 获取属性: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
所以这是我的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
});
#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
You can use Javascript
's dataset or JQuqery
data
method:
您可以使用Javascript的数据集或JQuqery数据方法:
$(document).ready(function() {
$('button').on('click', function() {
console.log(this.dataset.id, $(this).data().id, $(this).data('id'));
});
});
<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>
#5
0
Your code is correct, all need to do is pass this to your function like:
你的代码是正确的,所有需要做的就是将它传递给你的函数,如:
<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>
#6
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);
}
#1
8
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
然后使用传递的元素引用来获取data-id。您还可以使用像elem.dataset.id这样的HTMLElement.dataset属性
function ShowModal(elem){
var dataId = $(elem).data("id");
alert(dataId);
}
Additionally, I would recommend you use jquery to bind event handler's instead of ugly inline click handler.
另外,我建议你使用jquery绑定事件处理程序而不是丑陋的内联单击处理程序。
#2
3
One of the important part of jquery is manipulate the DOM.
jquery的一个重要部分是操纵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.
DOM =文档对象模型:文档对象模型(DOM)是一个平台和语言中立的接口,允许程序和脚本动态访问和更新文档的内容,结构和样式。
It has several DOM Manipulation :
它有几个DOM操作:
- Get Content : text(), html(), and val()
- 获取内容:text(),html()和val()
- Get Attributes : attr()
- 获取属性: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
所以这是我的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
});
#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
You can use Javascript
's dataset or JQuqery
data
method:
您可以使用Javascript的数据集或JQuqery数据方法:
$(document).ready(function() {
$('button').on('click', function() {
console.log(this.dataset.id, $(this).data().id, $(this).data('id'));
});
});
<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>
#5
0
Your code is correct, all need to do is pass this to your function like:
你的代码是正确的,所有需要做的就是将它传递给你的函数,如:
<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>
#6
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);
}