简体   繁体   English

我如何等到第一列中的所有表行都为100,才能在第二列中显示“完成”

[英]How can i wait until all table rows in the 1st column is 100 to display “done” in the 2nd column

I have a table which the number of row is dynamic depends on database record. 我有一个表,其中行数是动态的,取决于数据库记录。 So regardless of the no of table row, when all the rows in the first column is 100, all rows in the 2nd column will display "done". 因此,无论表中的行数是多少,当第一列中的所有行均为100时,第二列中的所有行都将显示为“完成”。

Right now, when i input 100 in the 1st row, all rows in the 2nd column will display "done". 现在,当我在第一行中输入100时,第二列中的所有行都将显示“完成”。 How can i wait until all rows in the 1st column is 100 to display "done" 我怎样才能等到第一列中的所有行都显示为100时

 $(".test").on('keyup', function() { var set = $('.test').val(); if (set == 100 ) { $('.result').val("done"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <table> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> 

Here is one-of-the-ways to achieve it. 这是实现它的一种方法。 Instead of keyup , you use input event. 您可以使用input事件来代替keyup

On every event, you check if all input fields have values or not. 在每个事件上,您都要检查所有输入字段是否都具有值。

If all fields have values, check if all have values as 100 or not. 如果所有字段都有值,请检查是否所有值都为100。

 $(".test").on('input', function() { checkAndUpdateSecondColumn(); }); function checkAndUpdateSecondColumn() { var empty = $("input.test").filter(function() { return this.value != ""; }); if ($("input.test").length == empty.length) { var sum = $('.test').toArray().reduce(function(sum, element) { return sum + Number(element.value); }, 0); $('.result').val(''); if (sum == $("input.test").length * 100) { $('.result').val('done'); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <table> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> <tr> <td><input type="number" class="test"> <td><input type="text" class="result"> </tr> 

The problem with your code is that when you assign your variable set , you are attempting to read the .val() of a jQuery collection containing multiple DOM elements. 代码的问题在于,当您分配变量set ,您试图读取包含多个DOM元素的jQuery集合的.val() The value of the first matching element is returned; 返回第一个匹配元素的值; the others are not evaluated. 其他未评估。

It sounds like what you want to do is evaluate the entire set and make certain that all match your desired value before displaying a "done" message. 听起来您想要做的是评估整个集合,并在显示“完成”消息之前确保所有这些都与您想要的值匹配。 One way to achieve this behavior is by filtering against the values of every element in the jQuery collection (every element of class "test" ). 实现这种行为的一种方法是通过对jQuery的集合(类每一个元素的每一个元素的值过滤"test" )。

var testInputs = $('.test');
var desiredValue = 100;

testInputs.on('keyup', function() {
    var testInputsSetToDesiredValue = testInputs.filter(function() {
        return parseInt(this.value, 10) === desiredValue;
    });
    if (testInputsSetToDesiredValue.length === testInputs.length) {
        $('.result').val('done');
    }
});

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

相关问题 如何搜索表的第二列而不是第一列 - How to search 2nd column of table instead of 1st column 我怎样才能做到第二<script> wait for 1st <script> to finish? - How can I make 2nd <script> wait for 1st <script> to finish? 通过与第二列进行比较来更改第二列的颜色 - Change the colour of the 2nd column by comparing it with the 1st column AngularJS搜索过滤器—第一个过滤器:全部,第二个过滤器:特定列 - AngularJS search filters — 1st filter : all , 2nd Filter :specific column 始终在屏幕上显示表格的第一行和第一列 - Always display 1st row and 1st column of a table on screen 为什么第二脚本阻止第一脚本重定向,我该如何解决? - Why does the 2nd script prevent the 1st script from redirecting and how can I fix it? 如何选择div表的第一列? - How to select 1st Column of div Table? 如何在不添加到第一个表的情况下将表行独占地添加到第二个表 - How to ADD Table Row to the 2nd table exclusively without adding to the 1st table 我这里有 2 个表格,在提交验证第一个表格后,我如何移动到第二个表格进行验证? - I have 2 forms here,after submitting validatting the 1st one, how can I move to the 2nd to validate? 如何将所选日期选择器值从第一页显示到第二页日期选择器 - How to display the selected datepicker value from the 1st page onto the 2nd page datepicker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM