[英]Sorting an HTML Table using one input field and one Select - Javascript
This script works for sorting an HTML Table using two standard input fields, but I am trying to use a select as the second input field and I cant seem to find a way to call the function onchange for the select specialtyid id to work as an input.此脚本适用于使用两个标准输入字段对 HTML 表进行排序,但我尝试使用 select 作为第二个输入字段,但我似乎无法找到一种方法来调用函数 onchange 以将 select specialid id 用作输入. Please help.请帮忙。 Thank you very much!非常感谢!
Here is the code:这是代码:
<script src="https://code.jquery.com/jquery-1.7.1.js"></script>
<input type="text" id="infoid" placeholder="Contractor Name">
<select id="specialtyid" onChange=" ">
<option value="">Select Contractor Type:</option>
<option value="Electrician">Electrician</option>
<option value="HVAC">HVAC</option>
<option value="Plumber">Plumber</option>
</select>
<p></p>
<table id="table">
<tr>
<td>Jack Doe</td>
<td>HVAC</td>
</tr>
<tr>
<td>Jack Doe JR</td>
<td>Plumber</td>
</tr>
<tr>
<td>Jane Doe</td>
<td>Electrician</td>
</tr>
<tr>
<td>Rick Pro</td>
<td>Plumber</td>
</tr>
</table>
$(window).load(function() {
var $rows = $('#table tr'),
info,
specialty;
$('input').keyup(function() {
infovalue = $('#infoid').val().toLowerCase(),
specialtyvalue = $('#specialtyid').val().toLowerCase();
$rows.each(function(index, tr) {
info = $(tr).find('td:nth-of-type(1)').text().toLowerCase(),
specialty = $(tr).find('td:nth-of-type(2)').text().toLowerCase();
if ((info.indexOf(infovalue) != -1) && (specialty.indexOf(specialtyvalue) != -1)) {
$(this).show();
} else {
$(this).hide();
}
});
if ((searchVal1 === '') && (searchVal2 === '')) {
$rows.show();
}
});
});
Here is few things to change:这里有几件事情需要改变:
#table
for table
更改table
的选择器#table
(infovalue === '') && (specialtyvalue === '')
更改条件以适合您的变量(infovalue === '') && (specialtyvalue === '')
$('#specialtyid').change(filterTable);
添加事件$('#specialtyid').change(filterTable);
$(window).load(function() { var $rows = $('table tr'), info, specialty; function filterTable(){ infovalue = $('#infoid').val().toLowerCase(), specialtyvalue = $('#specialtyid').val().toLowerCase(); $rows.each(function(index, tr) { info = $(tr).find('td:nth-of-type(1)').text().toLowerCase(), specialty = $(tr).find('td:nth-of-type(2)').text().toLowerCase(); if ((info.indexOf(infovalue) != -1) && (specialty.indexOf(specialtyvalue) != -1)) { $(this).show(); } else { $(this).hide(); } }); if ((infovalue === '') && (specialtyvalue === '')) { $rows.show(); } } $('input').keyup(filterTable); $('#specialtyid').change(filterTable); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <input type="text" id="infoid" placeholder="Contractor Name"> <select id="specialtyid" onChange=" "> <option value="">Select Contractor Type:</option> <option value="Electrician">Electrician</option> <option value="HVAC">HVAC</option> <option value="Plumber">Plumber</option> </select> <p></p> <table> <tr> <td>Jack Doe</td> <td>HVAC</td> </tr> <tr> <td>Jack Doe JR</td> <td>Plumber</td> </tr> <tr> <td>Jane Doe</td> <td>Electrician</td> </tr> <tr> <td>Rick Pro</td> <td>Plumber</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.