var elems = document.getElementsByClassName('items');
var myLength = elems.length,
totalsale = 0;
for (var i = 0; i < myLength; ++i) {
totalsale += elems[i].value;
}
document.getElementById('totalsale').value = totalsale;
มันออกมาเป็น 0 ค่ะอันนี้
Code (JavaScript)
function getItems()
{
var items = new Array();
var itemCount = document.getElementsByClassName("items");
var totalsale = 0;
var id= '';
for(var i = 0; i < itemCount.length; i++)
{
id = "saletotal_"+(i+1);
totalsale = totalsale + parseInt(document.getElementById(id).value);
}
document.getElementById('paymentreceipt').value = totalsale;
return totalsale;
}
getItems();
<script language="JavaScript">
function fncSums25()
{
var t1 = document.getElementsByName('textbox1');
var t2 = document.getElementsByName('textbox2');
var tot=0;
for(var i=0;i<enow.length;i++){
if(parseFloat(t1[i].value))
tot += parseFloat(t1[i].value)+parseFloat(t2[i].value);
}
var total=tot;
document.getElementById('textboxsum').value = Math.round(total);
}
let elems = document.getElementsByClassName('items');
let myLength = elems.length,
let totalsale = 0;
for (var i = 0; i < myLength; ++i) {
totalsale += elems[i].value-0; // elems[i].value-0 ทำ string ให้เป็นตัวเลข แบบสั้นๆ ขีเกียจ parse
}
document.getElementById('totalsale').value = totalsale;
Code (JavaScript)
function getType(x) {
let stringConstructor = "test".constructor;
let arrayConstructor = [].constructor;
let objectConstructor = ({}).constructor;
if (x === null) {return "null";}
if (x === undefined) {return "undefined";}
if (x.constructor === stringConstructor) {return "String";}
if (x.constructor === arrayConstructor) {return "Array";}
if (x.constructor === objectConstructor) {return "Object";}
return "don't know";
}
function getItems(){
let items = new Array();
let itemCount = document.getElementsByClassName("items");
let totalsale = 0;
for(var i = 0; i < itemCount.length; i++){
let id = `saletotal_${itemCount[i].id}`;
let v = document.getElementById(id).value
if(getType(v)==='String'){
totalsale += v-0;
}
}
document.getElementById('paymentreceipt').value = totalsale;
return totalsale;
}
getItems();