对jquery中的所有输入元素求和 [英] sum all input elements in jquery

查看:62
本文介绍了对jquery中的所有输入元素求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚如何编写一个jquery公式,该公式将求和所有在keyup上以"pull"开头的输入字段...我正在尝试下面的代码,但没有任何反应...没有错误,也没有更新要么...(HTML位于最底部)

Trying to figure out how to write a jquery formula that will sum all input fields that begin with "pull" on keyup... I'm trying the code below, but nothing happens... No errors, and no updates either.... (the html is at the very bottom)

$(document).ready(function(){
    /* sums pull total input fields */
    $("input[name^='pull']").bind("keyup", "calcPullTotal");
    calcPullTotal();
});


function calcPullTotal() {
    $("[id=totalpull]").calc(
        "pullnum + 0", { pullnum: $("input[name^=pull]") },
        function (s){
            return s.toFixed(0);
        },
        function ($this) {
            var sum = $this.sum();
                $("#totalpull").text(
                sum.toFixed(0)
            );
        }
    );  
}
<table id="convert">
<tbody>
<tr><td><input type="text" value="" name="pull0" /></td></tr>
<tr><td><input type="text" value="" name="pull1" /></td></tr>
<tr><td><input type="text" value="" name="pull2" /></td></tr>
<tr><td><input type="text" value="" name="pull3" /></td></tr>
</tbody>

<tfoot><tr><td><input type="text" id="totalpull" name="totalpull" value="" /></td></tr></tfoot>
</table>

推荐答案

尝试:

$("input[name^='pull']").bind("keyup", calcPullTotal);
calcPullTotal();

您正在将字符串"calcPullTotal"作为第二个参数传递给 bind 需要一个function.

You were passing the string "calcPullTotal" as the second argument to bind, which expects a function.

这篇关于对jquery中的所有输入元素求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆