从< td>获取价值第< th;匹配文字 [英] Get value from <td> where <th> matches text

查看:97
本文介绍了从< td>获取价值第< th;匹配文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我使用的系统,您可以创建自定义字段.这些字段都具有相同的html和css.现在,我想在星期几处获得td值.

With a system that I use you can create custom fields. These fields all have the same html and css. Now I would like to get the value td where the th is week.

现在我使用:

   var week = $(this).find('td.bug-custom-field:last').text();

哪个有效,但随后我必须确保它始终是最后一个字段.有更好的方法吗?

Which works but then I have to make sure it always is the last field. Is there a better way of doing this?

   <table>
   <tr>
   <th class="bug-custom-field category">Name</th>
   <td class="bug-custom-field" colspan="5">Test</td>
   </tr>

   <tr>
   <th class="bug-custom-field category">Week</th>
   <td class="bug-custom-field" colspan="5">23</td>
   </tr>
   </table>

推荐答案

变量1:

var week=0;
$('table tr td').each(function(){
	if($(this).prev().text()=='Week'){
		week=parseInt($(this).text(), 10);
	}
});
console.log(week);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<table>
   <tr>
     <th class="bug-custom-field category">Name</th>
     <td class="bug-custom-field" colspan="5">Test</td>
   </tr>

   <tr>
     <th class="bug-custom-field category">Week</th>
     <td class="bug-custom-field" colspan="5">23</td>
   </tr>
</table>

Var 2:

var week = $('table').find('td.bug-custom-field:last').text();
console.log(week);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
   <tr>
    <th class="bug-custom-field category">Name</th>
    <td class="bug-custom-field" colspan="5">Test</td>
   </tr>

   <tr>
    <th class="bug-custom-field category">Week</th>
    <td class="bug-custom-field" colspan="5">23</td>
   </tr>
</table>

这篇关于从&lt; td&gt;获取价值第&lt; th;匹配文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆