日期:2014-05-16 浏览次数:20365 次
function sum_total(){
var sum = 0;
$(":text[name^='price']").each(function(){
if($(this).val()){
sum += parseInt($(this).val());
}
});
$("priceTotal").value(sum);
}
<script type="text/javascript">
window.onload = function(){
document.getElementById('abv').onclick = function(){
var sum = 0;
for(var i = 1 ; i < 5 ; ++i){
sum += +document.getElementById('price'+i).value;
}
document.getElementById('priceTotal').value = sum;
}
}
</script>
<input type="text" name="priceTotal" id="priceTotal"/>
<input type="button" id="abv" value="测试用按钮"/>
<div id="div1">
<input type="text" name="price1" id="price1" />
<input type="text" name="price2" id="price2" />
<input type="text" name="price3" id="price3" />
<input type="text" name="price4" id="price4" />
</div>
var total = 0,i=1;
function sum_total(){
var ele = document.getElementById("price"+i);
i++;
if(ele){
if(!isNaN(ele.value)){
total += Number(ele.value);
}
sum_total();
}
}
sum_total();
document.getElementById("priceTotal").value = total;