[英]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.