I am currently using the code below to create a single-column search for an HTML table on a website. At the moment, when I type something in to the search bar, the code returns every row that has content that contains the thing I searched for anywhere within it. For example (I'm using this for movie ratings), when I type "G" in to the search bar, I get back everything rated G, but also everything rated PG or PG-13. Is there a way that I can adapt this code to return only results that are exact matches for my search? In other words, is there a way to change things so that when I search for "G" (or "g"), I only get back my G-rated movies and not anything rated PG or PG-13?
Thank you!
The Current Code:
<script type="text/javascript">
function searchRows(tblId) {
var tbl = document.getElementById(tblId);
var headRow = tbl.rows[0];
var arrayOfHTxt = new Array();
var arrayOfHtxtCellIndex = new Array();
for (var v = 0; v < headRow.cells.length; v++) {
if (headRow.cells[v].getElementsByTagName('input')[0]) {
var Htxtbox = headRow.cells[v].getElementsByTagName('input')[0];
if (Htxtbox.value.replace(/^\s+|\s+$/g, '') != '') {
arrayOfHTxt.push(Htxtbox.value.replace(/^\s+|\s+$/g, ''));
arrayOfHtxtCellIndex.push(v);
}
}
}
for (var i = 1; i < tbl.rows.length; i++) {
tbl.rows[i].style.display = 'table-row';
for (var v = 0; v < arrayOfHTxt.length; v++) {
var CurCell = tbl.rows[i].cells[arrayOfHtxtCellIndex[v]];
var CurCont = CurCell.innerHTML.replace(/<[^>]+>/g, "");
var reg = new RegExp(arrayOfHTxt[v] + ".*", "i");
if (CurCont.match(reg) == null) {
tbl.rows[i].style.display = 'none';
}
}
}
}
</script>
firstly use toUpperCase()
then you can just use ===
if you only want to distinguish G, PG, PG-13, R, NC-17
, you dont
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.