Hi I have the following code in a form . When the user clicks a button I want to get the current row in order to identify which of the buttons was clicked
你好,我有下面的代码。当用户单击一个按钮时,我想获取当前行,以确定单击了哪个按钮
<tr id="TEST1" >
<td align="left" valign="middle">
<div align="right">Contact</div>
</td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact1" size="20" /> Number
<input type="text" id="number1" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact1" />
</td>
</tr>
<tr id="TEST2" >
<td align="left" valign="middle">
<div align="right">Contact</div>
</td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact2" size="20" /> Number
<input type="text" id="number2" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
<tr id="TEST3" >
<td align="left" valign="middle">
<div align="right">Contact</div>
</td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact3" size="20" /> Number
<input type="text" id="number3" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
I thought the following Jquery would return the ID name but it doesn't
我以为下面的Jquery会返回ID名,但它不会
$('input[type=button]' ).click(function() {
bid = (this.id) ; // button ID
trid = $('tr').attr('id'); // table row ID
});
Can anyone give me some advice please ? thanks
有人能给我一些建议吗?谢谢
8 个解决方案
#1
63
You can use .closest()
to get up to the current <tr>
parent, like this:
您可以使用. nearest()函数来获取当前父函数,如下所示:
$('input[type=button]' ).click(function() {
var bid = this.id; // button ID
var trid = $(this).closest('tr').attr('id'); // table row ID
});
#2
7
Your code would be more like so:
你的代码会更像:
$('tr input[type=button]').click(function(){
id = $(this).closest('tr').attr('id');
});
#3
3
This:
这样的:
<table id="test">
<tr id="TEST1" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact1" size="20" /> Number
<input type="text" id="number1" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact1" /></td>
</tr>
<tr id="TEST2" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact2" size="20" /> Number
<input type="text" id="number2" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
<tr id="TEST3" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact3" size="20" /> Number
<input type="text" id="number3" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
and javascript:
和javascript:
$(function() {
var bid, trid;
$('#test tr').click(function() {
trid = $(this).attr('id'); // table row ID
alert(trid);
});
});
});
It worked here!
在这里工作!
#4
2
First, your jQuery will not work at all unless you enclose all your tr
s and td
s in a table:
首先,您的jQuery不能工作,除非您将所有trs和tds放在一个表中:
<table>
<tr>...</tr>
...
</table>
Second, your code gets the id
of the first tr
of the page, since you select all the tr
s of the page and get the id
of the first one (.attr()
returns the attribute of the first element in the set of elements it is used on)
其次,您的代码获得页面的第一个tr的id,因为您选择了页面的所有trs并获得了第一个trs的id (.attr()返回第一个元素的属性,该属性位于它所使用的元素的集合中)
Your current code:
你当前的代码:
$('input[type=button]' ).click(function() {
bid = (this.id) ; // button ID
trid = $('tr').attr('id'); // ID of the the first TR on the page
// $('tr') selects all trs in the DOM
});
trid is always TEST1 (jsFiddle)
Instead of selecting all tr
s on the page with $('tr')
, you want to select the first ancestor of the clicked upon input
that is a tr
. Use .closest()
for this in the form $(this).closest('tr')
.
与使用$(“tr”)选择页面上的所有trs不同,您希望选择一个tr的单击on输入的第一个祖先。
You can reference the clicked on element as this
, make a jQuery object out of it with the form $(this)
, so you have access to all the jQuery methods on it.
您可以像这样引用单击的on元素,使用form $(this)创建一个jQuery对象,这样您就可以访问它上的所有jQuery方法。
What your code should look like:
您的代码应该是这样的:
// On DOM ready...
$(function() {
$('input[type=button]' ).click(function() {
var bid, trid; // Declare variables. If you don't use var
// you will bind bid and trid
// to the window, since you make them global variables.
bid = (this.id) ; // button ID
trid = $(this).closest('tr').attr('id'); // table row ID
});
});
jsFiddle example
#5
1
$('input[type=button]' ).click(function() {
var bid = jQuery(this).attr('id'); // button ID
var trid = $(this).parents('tr:first').attr('id'); // table row ID
});
#6
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function getVal(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
alert(targ.innerHTML);
}
onload = function() {
var t = document.getElementById("main").getElementsByTagName("td");
for ( var i = 0; i < t.length; i++ )
t[i].onclick = getVal;
}
</script>
<body>
<table id="main"><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr><tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr></table>
</body>
</html>
#7
1
Create a class in css name it .buttoncontact, add the class attribute to your buttons
在css中创建一个类名为.buttoncontact,将class属性添加到按钮中
function ClickedRow() {
$(document).on('click', '.buttoncontact', function () {
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
});
}
#8
0
$('#tblCart tr').click(function () {
var tr_id = $(this).attr('id');
alert(tr_id );
});
<table class="table table-striped table-bordered table-hover" id="tblCart" cellspacing="0" align="center" >
<tr>
<th>
@Html.DisplayNameFor(model => model.Item.ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.Price.PriceAmount)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.Subtotal)
</th>
<th></th>
</tr>
@if (cart != null)
{
foreach (var vm in cart)
{
<tr id="@vm.Id">
<td>
@Html.DisplayFor(modelItem => vm.Item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Price.PriceAmount)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Subtotal)
</td>
<td >
<span style="width:80px; text-align:center;" class="glyphicon glyphicon-minus-sign" />
</td>
</tr>
}
}
</table>
#1
63
You can use .closest()
to get up to the current <tr>
parent, like this:
您可以使用. nearest()函数来获取当前父函数,如下所示:
$('input[type=button]' ).click(function() {
var bid = this.id; // button ID
var trid = $(this).closest('tr').attr('id'); // table row ID
});
#2
7
Your code would be more like so:
你的代码会更像:
$('tr input[type=button]').click(function(){
id = $(this).closest('tr').attr('id');
});
#3
3
This:
这样的:
<table id="test">
<tr id="TEST1" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact1" size="20" /> Number
<input type="text" id="number1" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact1" /></td>
</tr>
<tr id="TEST2" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact2" size="20" /> Number
<input type="text" id="number2" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
<tr id="TEST3" >
<td align="left" valign="middle"><div align="right">Contact</div></td>
<td colspan="4" align="left" valign="middle">
<input type="text" id="contact3" size="20" /> Number
<input type="text" id="number3" size="20" />
</td>
<td>
<input type="button" value="Button 1" id="contact2" />
</td>
</tr>
and javascript:
和javascript:
$(function() {
var bid, trid;
$('#test tr').click(function() {
trid = $(this).attr('id'); // table row ID
alert(trid);
});
});
});
It worked here!
在这里工作!
#4
2
First, your jQuery will not work at all unless you enclose all your tr
s and td
s in a table:
首先,您的jQuery不能工作,除非您将所有trs和tds放在一个表中:
<table>
<tr>...</tr>
...
</table>
Second, your code gets the id
of the first tr
of the page, since you select all the tr
s of the page and get the id
of the first one (.attr()
returns the attribute of the first element in the set of elements it is used on)
其次,您的代码获得页面的第一个tr的id,因为您选择了页面的所有trs并获得了第一个trs的id (.attr()返回第一个元素的属性,该属性位于它所使用的元素的集合中)
Your current code:
你当前的代码:
$('input[type=button]' ).click(function() {
bid = (this.id) ; // button ID
trid = $('tr').attr('id'); // ID of the the first TR on the page
// $('tr') selects all trs in the DOM
});
trid is always TEST1 (jsFiddle)
Instead of selecting all tr
s on the page with $('tr')
, you want to select the first ancestor of the clicked upon input
that is a tr
. Use .closest()
for this in the form $(this).closest('tr')
.
与使用$(“tr”)选择页面上的所有trs不同,您希望选择一个tr的单击on输入的第一个祖先。
You can reference the clicked on element as this
, make a jQuery object out of it with the form $(this)
, so you have access to all the jQuery methods on it.
您可以像这样引用单击的on元素,使用form $(this)创建一个jQuery对象,这样您就可以访问它上的所有jQuery方法。
What your code should look like:
您的代码应该是这样的:
// On DOM ready...
$(function() {
$('input[type=button]' ).click(function() {
var bid, trid; // Declare variables. If you don't use var
// you will bind bid and trid
// to the window, since you make them global variables.
bid = (this.id) ; // button ID
trid = $(this).closest('tr').attr('id'); // table row ID
});
});
jsFiddle example
#5
1
$('input[type=button]' ).click(function() {
var bid = jQuery(this).attr('id'); // button ID
var trid = $(this).parents('tr:first').attr('id'); // table row ID
});
#6
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function getVal(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
alert(targ.innerHTML);
}
onload = function() {
var t = document.getElementById("main").getElementsByTagName("td");
for ( var i = 0; i < t.length; i++ )
t[i].onclick = getVal;
}
</script>
<body>
<table id="main"><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr><tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr></table>
</body>
</html>
#7
1
Create a class in css name it .buttoncontact, add the class attribute to your buttons
在css中创建一个类名为.buttoncontact,将class属性添加到按钮中
function ClickedRow() {
$(document).on('click', '.buttoncontact', function () {
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
});
}
#8
0
$('#tblCart tr').click(function () {
var tr_id = $(this).attr('id');
alert(tr_id );
});
<table class="table table-striped table-bordered table-hover" id="tblCart" cellspacing="0" align="center" >
<tr>
<th>
@Html.DisplayNameFor(model => model.Item.ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.Price.PriceAmount)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.Subtotal)
</th>
<th></th>
</tr>
@if (cart != null)
{
foreach (var vm in cart)
{
<tr id="@vm.Id">
<td>
@Html.DisplayFor(modelItem => vm.Item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Price.PriceAmount)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => vm.Subtotal)
</td>
<td >
<span style="width:80px; text-align:center;" class="glyphicon glyphicon-minus-sign" />
</td>
</tr>
}
}
</table>