如何在知道确切的 'th' 文本的情况下获取正确的元素
How to get the proper element knowing the exact 'th' text
我需要从大 table 中的 select 获取 selected 值,只知道它之前的 th
元素的确切文本。在下面的摘录中,我需要得到 One
,只知道 check
。有什么想法吗?
<table>
....
<tr>
<th class="width2">check</th>
<td>
<select>
<option value="">- Select -</option>
<option value="1" selected="">One</option>
<option value="2">two</option>
</select>
</td>
...
</tr>
...
</table>
您可以使用 :contains()
selector but it select unwanted element in some cases. So use .filter()
过滤 th
有特殊文本。
var t = $("th").filter(function(){
return $(this).text() == 'check';
}).next().find("select :selected").text();
console.log(t);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th class="width2">check</th>
<td>
<select>
<option value="">- Select -</option>
<option value="1" selected="">One</option>
<option value="2">two</option>
</select>
</td>
</tr>
</table>
我需要从大 table 中的 select 获取 selected 值,只知道它之前的 th
元素的确切文本。在下面的摘录中,我需要得到 One
,只知道 check
。有什么想法吗?
<table>
....
<tr>
<th class="width2">check</th>
<td>
<select>
<option value="">- Select -</option>
<option value="1" selected="">One</option>
<option value="2">two</option>
</select>
</td>
...
</tr>
...
</table>
您可以使用 :contains()
selector but it select unwanted element in some cases. So use .filter()
过滤 th
有特殊文本。
var t = $("th").filter(function(){
return $(this).text() == 'check';
}).next().find("select :selected").text();
console.log(t);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th class="width2">check</th>
<td>
<select>
<option value="">- Select -</option>
<option value="1" selected="">One</option>
<option value="2">two</option>
</select>
</td>
</tr>
</table>