简体   繁体   English

警报自定义属性值jquery表单元格

[英]Alert custom attribute value jquery table cell

I need to get the attribute value but it says its undefined. 我需要获取属性值,但它表示未定义。

 $(document).ready(function() { $(".try").click(function() { alert($(this).attr("value")); }); }); 
 <table id="try" class="try" cellspacing="0" cellpadding="0"> <td style="background:#80FF80;" value="WD">WD</td> <td style="background:#FFFF80;" value="RD">RD</td> <td style="background:#879FDE;" valu ="OD">OD</td> </tr> </table> 

You have to include child element to your selector. 您必须在选择器中包含子元素。 Also your html isn't correct. 你的HTML也不正确。 You are missing open tr : 你缺少open tr

 $(".try tr td").click(function() { alert($(this).attr("value")); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="try" class="try" cellspacing="0" cellpadding="0"> <tr> <td style="background:#80FF80;" value="WD">WD</td> <td style="background:#FFFF80;" value="RD">RD</td> <td style="background:#879FDE;" value="OD">OD</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM