Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

sum all input elements in jquery

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...