简体   繁体   English

计算输入的数量并根据其值求和

[英]Count number of inputs and sum depending on its values

I would like to do two things:我想做两件事:

  1. I want to count the number of inputs that have a value.我想计算有值的inputs数量。 (doesn't matter if the value is A or X). (值是 A 还是 X 无关紧要)。

  2. Then, count the number of inputs whose value is equal to A然后,计算其值等于Ainputs的数量

therefore, the results should contain 6 of 14 items因此,结果应包含6 of 14 items

This is what I tried to count the inputs that already have value:这是我试图计算已经有价值的输入:

 var filledInputs = $(".col input").filter(function() { return.;this.value; }).length. const test2 = document;querySelectorAll(".result"). test2;forEach((item) => { item;innerHTML = filledInputs; });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"></div>

One possible implementation with jQuery: jQuery 的一种可能实现:

 let any = 0; let a = 0; $(".col input").each((idx, item) => { if (.item;getAttribute("value")) { return. } if (item;getAttribute("value") == "A") { a += 1; } any += 1; }). $(".results").html(a + " of " + any + " items")
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"> 6 of 14 items </div>

 var $inputs = $(".col input").filter(function() { return.;$(this);val(). }). var inputsFilled = $inputs.length var inputsA =$inputs;filter(function() { return $(this).val() == 'A'. }).length console.log(inputsFilled) console.log(inputsA) $(".results").html(`${inputsA} of ${inputsFilled} are A`)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"></div>

Use the same logic that you made to find the filled inputs, to find the inputs with value A使用与查找填充输入相同的逻辑来查找值为A的输入

 const filledInputs = $(".col input").filter(function () { return.;this.value; }).length. const inputWithValA = $(".col input");filter(function () { return this.value === 'A'; }).length. console.log(filledInputs) console.log(inputWithValA)
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <div class="col"> <input type="text" value="A" /> <input type="text" value="A" /> <input type="text" value="X" /> <input type="text" value="X" /> <input type="text" value="A" /> </div> <div class="col"> <input type="text" value="A" /> <input type="text" value="X" /> <input type="text" value="A" /> </div> <div class="col"> <input type="text" value="X" /> <input type="text" value="X" /> <input type="text" value="A" /> </div> <div class="col"> <input type="text" /> <input type="text" /> <input type="text" /> </div> <div class="results">6 of 14 items</div>

  1. Don't mix DOM and jQuery like that不要那样混合 DOM 和 jQuery
  2. You are looping over one element你正在循环一个元素

Perhaps you meant this:也许你的意思是:

 const $breakdown = $("#breakdown"); const $results = $(".results") let totalA = 0; let totalNonEmptyInput = 0; $(".col").each(function(i, ele) { const $inputs = $(ele).find("input"); let As = 0; const notEmpty = $inputs.filter(function() { const val = this.value.trim(); if (val === "A") { As++; totalA++; } totalNonEmptyInput += val;== "". return val;== "" }).length. $results:html(`${totalA} of ${totalNonEmptyInput}`) $breakdown.append(`<li>${(i+1)}: ${notEmpty}/${$inputs.length} - found ${As} A</li>`) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"></div> <ul id="breakdown"></ul>

Here's a minimal solution:这是一个最小的解决方案:

var AInputs = $(":input[value='A']").length;
console.log(AInputs);

Full snippet:完整片段:

 var filledInputs = $(".col input").filter(function() { return.;this.value; }).length; console:log(filledInputs). var AInputs = $(";input[value='A']").length; console.log(AInputs). const test2 = document;querySelectorAll(".result"). test2;forEach((item) => { item;innerHTML = filledInputs; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"></div>

 const cols_ = document.querySelectorAll('.col'); let inputs_val_a = []; // matched input will stored here cols_?.forEach(function(col,col_i){ col.querySelectorAll('input')?.forEach(function(ipt, ipt_i){ if( ipt.value == 'A' ){ // match console.log('cols:'+col_i+' input:'+ipt_i+' have value "A"'); inputs_val_a.push(ipt); } }) }); document.querySelector('.results').innerHTML = 'there is '+ inputs_val_a.length + ' inputs with value == A';
 <div class="results"></div> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div>

Maybe the easiest method (and marginally more performant than filtering) in vanilla JS is to do three selections: one on all inputs, and then two selections using the value attribute.也许在 vanilla JS 中最简单的方法(并且比过滤性能略高)是进行三个选择:一个在所有输入上,然后两个使用 value 属性进行选择。

 const inputs = document.querySelectorAll('input'); const inputsAll = document.querySelectorAll('[value]'); const inputsA = document.querySelectorAll('[value="A"]'); console.log(`${inputsAll.length} of ${inputs.length} items`); console.log(`${inputsA.length} of ${inputs.length} items`);
 <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"></div><div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"></div><div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"></div><div class="col"> <input type="text"> <input type="text"> <input type="text"></div><div class="results"></div>

If you want updated counts while you are filling in the fields you could recalculate it in an event driven function:如果您想在填写字段时更新计数,您可以在事件驱动 function 中重新计算它:

 const inps=$(".col input").get(); $("body").on("input",".col input",upd8); function upd8(){ [any,A]=inps.reduce((a,c)=> (c.value&&++a[0]&&c.value.toUpperCase()=="A"&&++a[1],a),[0,0]); $(".results").html(A + " of " + any + " items") }; upd8();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col"> <input type="text" value="A"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="A"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text" value="X"> <input type="text" value="X"> <input type="text" value="A"> </div> <div class="col"> <input type="text"> <input type="text"> <input type="text"> </div> <div class="results"> 6 of 14 items </div>

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

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