简体   繁体   English

您如何添加输入字段中的值并使用jQuery中的结果更新另一个字段?

[英]How do you add the values from input fields and update another field with the result in jQuery?

Preamble: I'm more of a PHP/MySQL guy, just starting to dabble in javascript/jQuery, so please excuse this dumb newbie question. 序言:我更像是一个PHP / MySQL的家伙,刚开始涉足javascript / jQuery,所以请原谅这个愚蠢的新手问题。 Couldn't figure it out from the Docs. 无法从文档中弄清楚。

I have a form without a submit button. 我有一个没有提交按钮的表格。 The goal is to allow the user to input values into several form fields and use jQuery to total them up on the bottom in a div. 目的是允许用户将值输入到多个表单字段中,并使用jQuery将值总计在div的底部。 The form kinda looks like this but prettier: 形式有点像,但更漂亮:

<form>
Enter Value: <input class="addme" type="text" name="field1" size="1">
Enter Value: <input class="addme" type="text" name="field2" size="1">
Enter Value: <input class="addme" type="text" name="field3" size="1">
etc.....
<div>Result:<span id="result"></span></div>
</form>

Is it possible to add these up? 可以将这些加起来吗? And if so, can it be done anytime one of the input fields changes? 如果是这样,是否可以在输入字段之一更改时随时进行?

Thanks. 谢谢。

UPDATE: Brian posted a cool collaborative sandbox so I edited the code to look more like what I have and it's here: http://jsbin.com/orequ/ to edit go here: http://jsbin.com/orequ/edit 更新: Brian发布了一个很棒的协作沙箱,所以我编辑了代码,使其看起来更像我所拥有的代码,它位于: http : //jsbin.com/orequ/进行编辑,请访问: http : //jsbin.com/orequ/edit

Sticking this right after the </form> tag should do it: 将其粘贴在</form>标记之后应该可以:

<script>
  function displayTotal() {
    var sum = 0

    var values = $('.addme').each(function(){
      sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
    });

    $('#result').text(sum);
  }
  $('.addme').keyup(displayTotal);
</script>

Here's a demo: http://jsbin.com/iboqo (Editable via http://jsbin.com/iboqo/edit ) 这是一个演示: http : //jsbin.com/iboqo (可通过http://jsbin.com/iboqo/edit编辑)

Try this: 尝试这个:

$(".addme").bind("change",function(){
    var _sum = 0;

    $(".addme").each(function(i){
        _sum += parseInt($(this).val());
    });

    $("#result").val(_sum);
});

Any non numeric or blank values will be disregarded in the calculation (they'll be given a value of zero and hence not affect the sum). 计算中将忽略任何非数字或空白值(它们将被赋予零值,因此不会影响总和)。

function sumValues() {
    var sum = 0;

    $("input.addme").each(function(){
        var $this = $(this);
        var amount = parseInt($this.val(), 10);
        sum += amount === "" || isNaN(amount)? 0 : amount;
    });

    $("#result").text(sum);
}

$(function() {

  sumValues();

  $("input.addme").keyup(function(){
    sumValues();
  });

});

Working Demo 工作演示

(function(){
    var context = $("form"), elements = $("input.addme", context);
    function getSum(elements) {
        var sum = 0;
        $(elements, context).each( function() {
            var v = parseInt(this.value);
            v === parseInt(v,10) ? sum += v : sum = sum;
        })
        return sum;
    }
    $(elements).bind("keyup", function() {
        $("#result").text( getSum(elements) );
    });
})();

isolated scope and context, included dealing with non-integer values, function getSum should rather return a value than do something itself. 隔离的范围和上下文(包括处理非整数值),函数getSum宁可返回值,也不愿自己做某事。

暂无
暂无

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

相关问题 如何获取2个数字输入字段的值,然后使用Jquery进行除法并将结果显示给另一个输入? - How do i take values of 2 input fields which are numbers, then divide using Jquery and display the result to another input? 如何在隐藏的输入字段中添加和删除值? - How do I add & remove values from a hidden input field? 如何添加一旦输入值将更新的输入字段? - How to add input fields that will update once values are entered? 如何使用javascript更新隐藏输入字段的值 - How do I use javascript to update the values of hidden input fields 如何使用Angular JS在带有输入字段的文本框中添加文本并将其从文本框中删除? - How do you add text to a text box with an input field and delete it from the text box with Angular JS? 如何使用jQuery替换输入字段的特定值? - How do you replace a specific value of an input field using jQuery? 如何使用JavaScript向输入字段添加值? - How do I add values to input fields using JavaScript? 如何使用jquery复制修改后的输入值 - How do you copy modified input values with jquery 如何从下拉列表中选择多个值,并使用JavaScript或jQuery将其添加到另一个字段 - How to select multiple values from dropdown and add it to another field using JavaScript or jQuery 如何在删除字段时计算输入字段值并将其分配给另一个字段 - How to calculate input fields values and assign it to another field while removing a field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM