[英]Get value of TD in Jquery
I have multiple tr
on my page,that will have tds
in that.Now every tr have a td that have bd_cl
i want to get the value of bd_cl
on click of of the link 我的页面上有多个tr
,其中将包含tds
。现在每个tr都有一个具有bd_cl
的td,我想在点击链接时获取bd_cl
的值
HTML HTML
<tr>
<td>121</td>
<td>111</td>
<td>11</td>
<td bd_cl="PE">Address</td>
<td>
<a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="/icon_edit.gif"></a>
</td>
</tr>
<tr>
<td>121</td>
<td>111</td>
<td>11</td>
<td bd_cl="AD">Name</td>
<td>
<a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="/icon_edit.gif"></a>
</td>
</tr>
jQuery jQuery的
function uodate (objref){
var $parenttrobj = $(objref).closest('tr');
alert($parenttrobj.attr("bd_cl").val());
}
But i am not able to get the value .Kindly help me in this 但是我无法获得价值。请在这方面帮助我
You have wrong selector for targeting clicked anchor element parents previous td element. 您选择了错误的选择器来定位单击的锚元素父元素之前的td元素。 use correct selector along with .attr("bd_cl")
to get the value: 使用正确的选择器以及.attr("bd_cl")
来获取值:
function update (objref){
var $parenttdobj = $(objref).parent();
alert($parenttdobj.prev().attr("bd_cl"));
}
You can use the following code 您可以使用以下代码
$('td').each(function(){
if($(this).hasAttr('bd_cl')){
alert($(this).text());
}
});
You have a typo in your function name. 您的函数名称中有一个错字。 Also, you missed your table
tags. 另外,您错过了table
标签。
If you are want to get attribute value just use .attr('bd_cl')
如果要获取属性值,请使用.attr('bd_cl')
function update (objref){
var $parenttrobj = $(objref).closest('tr').find('td[bd_cl]');
alert($parenttrobj.attr("bd_cl"));
}
If you are want to get td
content: 如果要获取td
内容:
function update (objref){
var $parenttrobj = $(objref).closest('tr').find('td[bd_cl]');
alert($parenttrobj.text());
}
function update(el) { var td = $(el).parent().prev(); alert(td.attr('bd_cl')); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>121</td> <td>111</td> <td>11</td> <td bd_cl="PE">Address</td> <td> <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="//placehold.it/32x32"></a> </td> </tr> <tr> <td>121</td> <td>111</td> <td>11</td> <td bd_cl="AD">Name</td> <td> <a class="edit" title="Edit User" onclick="update(this)" href="javascript:void(0);"><img border="0" src="//placehold.it/32x32"></a> </td> </tr> </table>
use find
in jquery 在jQuery中使用find
var $parenttrobj = $(objref).closest('tr');
alert($parenttrobj.find("td").attr("bd_cl"));
I'd suggest revising your approach, slightly, to move away from the in-line event-handlers, use jQuery to assign click-handling function: 我建议稍微修改一下您的方法,以脱离嵌入式事件处理程序,使用jQuery分配点击处理功能:
function update() { // 'this' here is the element that was interacted with // at the point of event-handling being assigned. // find the closest ancestor <tr> element, // find the the <td> elements that have a 'bd_cl' attribute: var cell = $(this).closest('tr').find('td[bd_cl]'); // use console.log() rather than 'alert' to log the retrieved text: console.log(cell.text()); } // adds a click-handling function to the <a class="edit"> elements // that are within <td> elements. jQuery will pass the clicked // element to the assigned function as 'this': $('td a.edit').on('click', update);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td>121</td> <td>111</td> <td>11</td> <td bd_cl="PE">Address</td> <td> <a class="edit" title="Edit User" href="#"> <img border="0" src="/icon_edit.gif"> </a> </td> </tr> <tr> <td>121</td> <td>111</td> <td>11</td> <td bd_cl="AD">Name</td> <td> <a class="edit" title="Edit User" href="#"> <img border="0" src="/icon_edit.gif"> </a> </td> </tr> </tbody> </table>
As you were trying to find the text contained within a <td>
element, noticeably not an <input />
or <select>
element, it has no val()
method available to it; 当您试图查找包含在<td>
元素(显然不是 <input />
或<select>
元素)中的文本时,它没有可用的val()
方法。 instead text()
must be used. 相反,必须使用text()
。
Further, while this will work, it's worth pointing out that your HTML is invalid, due to the custom attributes you're using. 此外,尽管这将起作用,但值得指出的是,由于使用的自定义属性,您的HTML无效。 Under HTML 5, however, you have access to valid data-*
custom attributes which will validate under the HTML 5 doctype ( <doctype html>
), though it remains just as invalid (albeit backwards compatible) with HTML 4.x. 但是,在HTML 5下,您可以访问有效的data-*
自定义属性,这些属性将在HTML 5 doctype( <doctype html>
)下进行验证,尽管它仍然与HTML 4.x一样无效(尽管向后兼容)。 So, to use data-*
attributes: 因此,要使用data-*
属性:
function update() { var cell = $(this).closest('tr').find('td[data-bd_cl]'); console.log(cell.text()); } $('td a.edit').on('click', update);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td>121</td> <td>111</td> <td>11</td> <td data-bd_cl="PE">Address</td> <td> <a class="edit" title="Edit User" href="#"> <img border="0" src="/icon_edit.gif"> </a> </td> </tr> <tr> <td>121</td> <td>111</td> <td>11</td> <td data-bd_cl="AD">Name</td> <td> <a class="edit" title="Edit User" href="#"> <img border="0" src="/icon_edit.gif"> </a> </td> </tr> </tbody> </table>
References: 参考文献:
closest()
. closest()
。 find()
. find()
。 [attribute]
) selector . 具有属性( [attribute]
)选择器 。 on()
. on()
。 text()
. text()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.