如何获取被点击元素的ID
How to get the clicked element's ID
我想检索被点击元素的 ID(在 'onclick; 事件上。
<script>
function myFunction(element)
{
var $id = element.id;
$($id).click(function() {
$.get('ajaxTestSrvlt?test=1', function(responseText) {
$('#firstName').val(responseText); with the response text.
});
});
};
</script>
这是调用此函数的元素。我想从上面的脚本中获取这个原始文件的 ID。这仅在我直接将 "clickableRow" 作为 id 提供给脚本时才有效。 "ID" 将动态更改
<tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal" onclick="myFunction(this)">
试试这个
function myFunction(ele)
{
var id=$(ele).attr("id");
$('#'+id).click(function() {
//code here
});
}
就用这个
onclick="myFunction(this.id)
而不是
onclick="myFunction(this)
在我的函数中获取你的 id
function myFunction(getid) {
alert(getid);
}
无需内联事件处理程序
<tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal">
然后
jQuery(function () {
$('.clickable-row').click(function () {
var $this = $(this),
id = this.id;
$.get('ajaxTestSrvlt?test=1', function (responseText) {
$('#firstName').val(responseText);
//here `id` is the id of the clicked tr
});
});
})
我想检索被点击元素的 ID(在 'onclick; 事件上。
<script>
function myFunction(element)
{
var $id = element.id;
$($id).click(function() {
$.get('ajaxTestSrvlt?test=1', function(responseText) {
$('#firstName').val(responseText); with the response text.
});
});
};
</script>
这是调用此函数的元素。我想从上面的脚本中获取这个原始文件的 ID。这仅在我直接将 "clickableRow" 作为 id 提供给脚本时才有效。 "ID" 将动态更改
<tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal" onclick="myFunction(this)">
试试这个
function myFunction(ele)
{
var id=$(ele).attr("id");
$('#'+id).click(function() {
//code here
});
}
就用这个
onclick="myFunction(this.id)
而不是
onclick="myFunction(this)
在我的函数中获取你的 id
function myFunction(getid) {
alert(getid);
}
无需内联事件处理程序
<tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal">
然后
jQuery(function () {
$('.clickable-row').click(function () {
var $this = $(this),
id = this.id;
$.get('ajaxTestSrvlt?test=1', function (responseText) {
$('#firstName').val(responseText);
//here `id` is the id of the clicked tr
});
});
})