如何为for循环添加数值

时间:2015-03-13 08:54:34

标签: javascript

我已经将数值传递给javascript function.i想要检查它for loop.val是checkboxes.in这个thime有两个checkboxes.then val是2

function cal(val, id){ // val =2
    var sum = 0;
    var gn, elem;
    var l = 1 * val; //l=1*2
    var b = parseInt(l) + 1; // b=2+1
    for (i = 1; i < b; i++) { //this is not working.but b to replace 3,it's working
        alert(i);
        gn = 'testw' + i; //checkboxes ids
        elem = document.getElementById(gn);
        if (elem.checked == true) {
            sum += Number(elem.value); //calculate checkboxes values
        }
    }
    document.getElementById("rp").value = sum.toFixed(2);
    // display total in textfield
}

0 个答案:

没有答案