简体   繁体   English

如何使用Javascript知道行号时从表行获取标签值

[英]How to get the label value from table row when row number is known, using Javascript

I have a table like this- 我有一张这样的桌子 -

<table>
 <tr>
   <td>
      <label id="lbl1" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
 <tr>
   <td>
      <label id="lbl2" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
 <tr>
   <td>
      <label id="lbl3" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
</table>

My problem is that I want to alert the value of label present in the second row's first column. 我的问题是我想提醒第二行第一列中出现的标签值。 Assume that I don't know label id means I know its pattern like lbl1,lbl2 or lbl3.. but not exactly what it is in the second row. 假设我不知道label id意味着我知道它的模式如lbl1,lbl2或lbl3 ..但不完全是第二行中的模式。

You can use something like next 你可以使用下一个

var labels = document.getElementsByTagName("label");
for (var i=0; i<labels.length; i++)
   if (labels[i].id && labels[i].id.indexOf("lbl") == 0){
      //you have found the label in the first row
   }

If you are okay to use jQuery use this fiddle 如果你可以使用jQuery使用这个小提琴

var label = $('table tr:eq(1) td:eq(0)').find("label").attr("value")
alert(label);

You Can get label value by class name 您可以按类名获取标签值

$("label[class=lblclass]").each(function() {var result= $(this).val(); });

(OR) You can get the Particular Label Value by ID (或)您可以按ID获取特定标签值

function getlabel_value(){var result=$('#lbl1').val();}

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

相关问题 如何使用JavaScript从表中的行Index获取行id - How to get row id from row Index in table using JavaScript Javascript生成的表 - 当该行中的数字输入值发生变化时,如何更新该行中的其他单元格 - Javascript generated table - How to update other cells in a row when a number input value in this row changes 如何使用javascript连续获取HTML表格单元格上的Dropdownlist值 - How to get Dropdownlist value on HTML Table Cell in a Row using javascript 如何使用 Javascript 获取表格行中的第二个复选框值 - How to Get the Second Checkbox Value in a Table Row Using Javascript 如何从javascript中的表中获取列名和第一行值? - How to get column name and first row value from table in javascript? 如何从数据库表中获取行值以在JavaScript / jQuery中使用它? - How to get a row value from database table to use it in JavaScript / jQuery? 如何从JavaScript的附加表中获取每一行的ID值 - how to get the id value in each row from the append table in JavaScript 如何使用绝对行号从复选框中使用jQuery获取表单元格的值 - How to get value of table cell with jquery from checkbox using absolute row number 如何使用javascript从html中的表格中获取选定的行? - How to get selected row from a table in html using javascript? 单击表行时如何从单元格获取值 - How to get the value from a cell when table row is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM