Javascript 为自动求和功能创建除零以外的起点
我有一个自动求和单元格,当前从0开始。我要求和的单元格是输入框,但列表中也有静态框。我想在自动求和功能中包含静态框,或者创建除0以外的其他起点。这是我目前的代码:Javascript 为自动求和功能创建除零以外的起点,javascript,jquery,Javascript,Jquery,我有一个自动求和单元格,当前从0开始。我要求和的单元格是输入框,但列表中也有静态框。我想在自动求和功能中包含静态框,或者创建除0以外的其他起点。这是我目前的代码: $(document).ready(function(){ //iterate through each textboxes and add keyup //handler to trigger sum event $('.budget').each(function() { $(this
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$('.budget').each(function() {
$(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$('.budget').each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#total").html(sum.toFixed(0));
}
将初始值传递给函数如何:
function calculateSum(params) {
var sum = params && params.init || 0;
...
}
calculateSum({ init: 12 });
你忘了解释是什么阻止了你这么做。另外,我在这里没有看到java。你意识到java和Javascript是两个完全不相关的东西,对吗?请从你的问题中删除
java
标记。阅读本页了解有关创建好问题的更多信息: