简体   繁体   English

使用 JavaScript 按 HTML 表中的下拉菜单值过滤

[英]Filtering by drop down menu value in an HTML table using JavaScript

I have created a very simple table in HTML.我用 HTML 创建了一个非常简单的表格。 The column "Sector" has 4 possible selections in a drop down list. “扇区”列在下拉列表中有 4 个可能的选择。 I type manually the amount of money in the second column.我在第二列中手动输入了金额。 What I want to do is to display Total Money in the third column, which should be:我想要做的是在第三列中显示 Total Money,应该是:

Total money = (Amount of money) * 1, in case that Sector = Footbal (and therefore value "s1") Total money = (Amount of money) * 1, 如果 Sector = Footbal(因此值“s1”)

Total money = (Amount of money) * 9, in any other case总金额=(金额)* 9,在任何其他情况下

For some reason it multiplies always by 9. How can I make this work?出于某种原因,它总是乘以 9。我怎样才能做到这一点?

在此处输入图片说明

 function getValue() { var selectedValue1 = document.querySelectorAll(".sector"); if (selectedValue1 == 's1') { CONSTANT = 1; } else { CONSTANT = 9; } } var Initial = document.querySelectorAll('.number1'); var Double = document.querySelectorAll('.number2'); Initial.forEach(myFunction); function myFunction(item, index) { getValue() item.addEventListener('change', (event) => { var initialValue = event.target.value; Double[index].value = initialValue * CONSTANT; }); }
 <! --COMIENZO TABLA --> <table class="egt"> <! --SEGUNDA LINEA --> <tr> <th>Sector</th> <th>Amount of money</th> <th>Total money</th> </tr> <! --TERCERA LINEA --> <tr> <td> <select class="sector"> <option value="s1">Football</option> <option value="s2">Basketball</option> <option value="s3">Volleyball</option> <option value="s4">Rugby</option> </select> </td> <td> <input type="number" class="number1"> </td> <td> <input type="number" class="number2"> </td> </tr> <! --CUARTA LINEA --> <tr> <td> <select class="sector"> <option value="s1">Football</option> <option value="s2">Basketball</option> <option value="s3">Volleyball</option> <option value="s4">Rugby</option> </select> </td> <td> <input type="number" class="number1"> </td> <td> <input type="number" class="number2"> </td> </tr> </table> <! --FINAL TABLA -->

You should delegate and get the value from the select on change您应该委托并从更改中的选择中获取值

I added thead and tbody我添加了 thead 和 tbody

 const table = document.querySelector(".egt"); table.addEventListener("input", function(e) { // when ANYTHING changes [...table.querySelectorAll("tbody tr")].forEach(row => { // loop over all rows const sectorValue = row.querySelector(".sector").value == 's1' ? 1 : 9; // from this row const num1 = row.querySelector(".number1").value; row.querySelector(".number2").value = num1 * sectorValue; }); })
 <! --COMIENZO TABLA --> <table class="egt"> <thead> <! --SEGUNDA LINEA --> <tr> <th>Sector</th> <th>Amount of money</th> <th>Total money</th> </tr> </thead> <! --TERCERA LINEA --> <tbody> <tr> <td> <select class="sector"> <option value="s1">Football</option> <option value="s2">Basketball</option> <option value="s3">Volleyball</option> <option value="s4">Rugby</option> </select> </td> <td> <input type="number" class="number1"> </td> <td> <input type="number" class="number2"> </td> </tr> <! --CUARTA LINEA --> <tr> <td> <select class="sector"> <option value="s1">Football</option> <option value="s2">Basketball</option> <option value="s3">Volleyball</option> <option value="s4">Rugby</option> </select> </td> <td> <input type="number" class="number1"> </td> <td> <input type="number" class="number2"> </td> </tr> </tbody> </table> <! --FINAL TABLA -->

NOTE: I use the spread operator [...collection] to convert an HTML Collection into an array - it is to handle older EDGE browsers which do not have native forEach on a collection.注意:我使用展开运算符[...collection]将 HTML 集合转换为数组 - 它用于处理在集合上没有原生 forEach 的旧版 EDGE 浏览器。 You can do table.querySelectorAll("tbody tr").forEach() if you do not care to support older Edge如果您不关心支持较旧的 Edge,您可以执行table.querySelectorAll("tbody tr").forEach()

Your cade has some fundamental problems (fe since selectedValue1 is a Node list, it can't be equal to "s1"), but also your approach is incorrect.您的 cade 有一些基本问题(fe 因为 selectedValue1 是一个节点列表,它不能等于“s1”),而且您的方法也不正确。

I don't see any reason to create loop here, but if need it:我看不出有任何理由在这里创建循环,但如果需要的话:

Loop through each row, and then get the value of selection as well as number, and calculate the output and set it.循环遍历每一行,然后获取选择的值以及数字,并计算输出并设置它。 This is easier and the correct way.这是更简单和正确的方法。

First, Give your table thead and tbody tags to create a wrapper around data.首先,给你的表 toad 和 tbody 标签创建一个围绕数据的包装器。

let CONSTANT = 1;
let rows = document.querySelectorAll("tbody tr");
[...rows].forEach(row => {
   let selection = row.querySelector(".sector").value;
   CONSTANT = selection === "s1" ? 1 : 9;
   let number = row.querySelector(".number1").value;
   row.querySelector(".number2").value = CONSTANT * number;
})

But this is also not the best way.但这也不是最好的方法。 Instead of looping, just add event listeners and call a function to calculate.无需循环,只需添加事件侦听器并调用函数进行计算。

let rows = document.querySelectorAll("tbody tr");
[...rows].forEach(row => {
   row.querySelector(".number1").addEventListener("input", () => {
     calculate(row)
   })
   row.querySelector(".sector").addEventListener("change", () => {
      calculate(row)
   });
})

function calculate(row) {
    let constant = row.querySelector(".sector").value === "s1" ? 1 : 9;
    let num1 = row.querySelector(".number1").value;
    row.querySelector(".number2").value = constant * num1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM