简体   繁体   中英

Document Object Model issues

ok, I"m having serious problems with the DOM when I do ANY kind of Javascript. the following snippet of code doesn't work, for some reason that I can't begin to fathom. Whenever I try to use the getElementById() function, the script stops working. please tell me what I'm doing wrong.

var total=0;
document.write("test");
function quickTotal(price,id){
    alert(price)
    alert(id)
    var object=getElementById(id)
    if(object.checked == 1){
        total=parseFloat(total)+parseFloat(price)
        alert("add")
    }
    if(object.checked == 0){
        total=parseFloat(total)-parseFloat(price)
        alert("subtract")
}

    alert(total)
    //document.floater.price.innerHTML("test")
}

Try using document.getElementById(id) (prefix it with document , since the method is on the document object, not window ).

Update: an example with your code:

<input type="checkbox" id="myChkBox" />
<input type="button" onclick="quickTotal(30, 'myChkBox');" value="Click me" />

<script type="text/javascript">
    var total = 0;
    document.write("test");
    function quickTotal(price, id) {
        alert(price);
        alert(id);
        var object = document.getElementById(id);
        if (object.checked == 1) {
            total = parseFloat(total) + parseFloat(price);
            alert("add");
        }
        if (object.checked == 0) {
            total = parseFloat(total) - parseFloat(price);
            alert("subtract");
        }

        alert(total);
        //document.floater.price.innerHTML("test")
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM