简体   繁体   English

使用一个输入字段和一个 Select 对 HTML 表格进行排序 - Javascript

[英]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:这里有几件事情需要改变:

  1. Change the selctor #table for table更改table的选择器#table
  2. Change the condition to fit with your variables (infovalue === '') && (specialtyvalue === '')更改条件以适合您的变量(infovalue === '') && (specialtyvalue === '')
  3. Create a function to attach on both events创建一个函数来附加两个事件
  4. Add the event $('#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.

相关问题 使用 Javascript 从 html 输入字段添加和排序整数数组 - Adding And Sorting Array Of Integers from html input field Using Javascript Javascript &amp; HTML 单位转换器 - 如何使用一个输入字段作为常数 - Javascript & HTML unit converter - how to use one input field as constant 如何在没有焦点的情况下使用javascript读取html页面中的输入 - How to read input in a html page with javascript without focus in one field 使用php和javascript验证html中的多选输入字段 - validate a multi select input field in html using php and javascript 将输入和选择组合到一个可见的输入字段中 - Combining input and select into one visible input field 使用 Javascript 动态替换 HTML 表单字段的最佳方法,但输入同步到原始/隐藏字段 - Best way to dynamically replace a HTML Form Field with a different one using Javascript, but have input sync to the original/hidden field 如何将输入字段值相加并使用Javascript将其显示到html表中? - How to sum the input field values and display it into html Table using Javascript? 使用选择选项菜单对表进行排序 - table sorting javascript using select option menu 在一个HTML页面中使用JavaScript计算2组输入类型 - Calculate 2 sets of input types using javascript in one html page 使用Javascript按行数据对表进行排序 - Sorting HTML table by row data using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM