简体   繁体   English

jQuery仅循环其中具有值的那些输入

[英]Jquery loop through only those inputs which has values in it

I am trying to loop through form fields and add the value of each input and show the total in another div. 我试图遍历表单字段并添加每个输入的值,并在另一个div中显示总计。 It is working for me only if I fill all the form fields. 仅当我填写所有表单字段时,它才对我有用。 How I can make it work if I have values only in my 2 form fields? 如果仅在2个表单字段中有值,如何使它起作用?

HTML: HTML:

<ul class="nav tabs " id="calculate">
      <li class="active"><input type="text" class="col-sm-3 pull-right" id="right-input"></li>
      <li><a href="#tab2" data-toggle="tab">    <input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab3" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab4" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab5" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab6" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab7" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab8" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab9" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
      <li><a href="#tab10" data-toggle="tab"><input type="text" class="col-sm-3 pull-right" id="right-input"></a></li>
</ul>

JS: JS:

$(document).ready(function() 
{
    $("input").keyup(function() 
    {
        var $inputs = jQuery('#calculate :input');
        var values = {};
        var total = 0;
        $inputs.each(function() 
        {
            if(values[this.name] !== "") 
            {
                total = parseFloat(total) + parseFloat(values[this.name]);
            }
            if(total)
        });
        console.log(total);
    });
});

您可以在循环前过滤输入,这将为您提供值不为空的输入。

 var $inputs = jQuery('#calculate :input').filter(function(){return this.value !==''});

You have to do little change in you each() loop like this: 您只需在您的each()循环中进行一些更改,如下所示:

$inputs.each(function () {
         if ($(this).val() !== "") {
             total = parseFloat(total) + parseFloat($(this).val());
         }  
     });

FIDDLE DEMO 现场演示

You need to change how you add up your values, try this: 您需要更改汇总值的方式,请尝试以下操作:

var value = !isNaN(Number(values[this.name])) ? parseFloat(values[this.name]) : 0;
total += value;

暂无
暂无

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

相关问题 jQuery 循环表单并仅检索可见输入的字段标签和值 - jQuery loop through form and retrieve field labels and values of visible inputs only 如何仅捕获用户更改值的多行中的那些列? - How do I capture only those columns in multiple rows in which user has changed values? jQuery:如何遍历一组仅查找与另一个数组中的值匹配的元素? - jQuery: How can I loop over a set of elements finding only those matching the values within another array? Js - 遍历数组以创建仅填充最后一项输入值的输入和值 - Js - Loop through array to create inputs and values only filling last item input value jQuery-检查特定输入是否为空值,并将类仅添加到这些输入为空 - jquery - Check specific inputs for empty value and add class to ONLY those inputs that are empty 在for循环内检查无线电输入(仅包含值的输入) - Checked radio input (only those which contain a value) inside a for loop 如何通过 HTML 获取输入,然后使用这些输入与 javascript 运行循环? - How do I take inputs through HTML and then use those to run a loop with javascript? 如何仅传递在 POST 请求正文中更改的那些值 - how to pass only those values which changed in POST request body 使用$ .post()将PHP数组发送到jQuery,然后循环遍历这些值 - Sending PHP Array to jQuery with $.post() and then looping through those values 通过多个输入循环并使用jQuery将答案保存到变量 - Loop through multiple inputs and save the answer to a variable using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM