我需要在表頭中找到 0.10、.20、.30 到 0.50。搜索引數基于公式,最終將確定要過濾的列。我從以前的類似帖子中找到了這個片段,但是當我插入我的表格時,我不會得到任何結果。
找到正確表頭的正確方法是什么?
$(function() {
$('#search').click(function() {
$('td').each(function(index) {
if ($(this).text() == $('#lookup').val()) {
console.log(index)
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table id="myTable">
<tr class="header" id="ecentric">
<th style="width:8%;">Column</th>
<th style="width:20%;">Plate/saddle</th>
<th style="width:10%;">Beam Width</th>
<th style="width:10%;">0</th>
<th style="width:10%;">.10</th>
<th style="width:10%;">.20</th>
<th style="width:10%;">.30</th>
<th style="width:8%;">.40</th>
<th style="width:8%;">.50</th>
</tr>
<input type="text" id="lookup">
<input type="button" id="search" value="search">
uj5u.com熱心網友回復:
添加標題
搜索 th 而不是 td
快取選定的 TH
更具體地使用您的選擇器,使用
$('#myTable thead th')而不是,$('th')因為您可能會針對 DOM 中不需要的TH元素您甚至可以跳過前 3 個使用
$('#myTable thead th:nth-child(n 3)') ... if (index !=-1) console.log(index 3)
$(function() {
const $ths = $('#myTable thead th');
$('#search').on("click", function() {
$ths.each(function(index) {
if ($(this).text() == $('#lookup').val()) {
console.log(index)
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table id="myTable">
<thead>
<tr class="header" id="ecentric">
<th style="width:8%;">Column</th>
<th style="width:20%;">Plate/saddle</th>
<th style="width:10%;">Beam Width</th>
<th style="width:10%;">0</th>
<th style="width:10%;">.10</th>
<th style="width:10%;">.20</th>
<th style="width:10%;">.30</th>
<th style="width:8%;">.40</th>
<th style="width:8%;">.50</th>
</tr>
</thead>
</table>
<input type="text" id="lookup">
<input type="button" id="search" value="search">
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/476354.html
標籤:javascript html jQuery
