-1

我有一些 javascript 行,如果它从同一系列名称中获取价值,则效果很好。但是后来当每个值传递到另一个页面时我遇到了一个问题,我想分解哪个值属于哪个页面。所以问题是如何将脚本计算值的方式从“name”更改为“id”。如下代码:

<script type="text/javascript">
    //auto commas
    function doThousands(n) {
        n = '' + n;
        if (n.length < 4) return n;
        var c = n.length % 3;
        var pre = n.substring(0, c);
        return pre + (pre.length? ',' : '') + n.substring(c).match(/\d{3}/g).join(',');
    }
//sub total
    function checkTotal() {
        document.cc_form.total.value = '';
        var sum = <?=$days*$_rate*$_rooms?>;
        for (i=0;i<document.cc_form.cost.length;i++) {
            if (document.cc_form.cost[i].checked) {
                sum = sum + parseInt(document.cc_form.cost[i].value);
                }
            }document.cc_form.total.value = doThousands(sum);
        }
    </script>

这是 HTML:

<form name="cc_form" id="cc_form" method="post" action="/">
<label for="transfer1"><input type="checkbox" id="transfer1" name="cost" value="800" autocomplete="off" onchange="checkTotal()" />&nbsp;Taxi (800 THB | 2 pax)</label><br />
<label for="transfer2"><input type="checkbox" id="transfer2" name="cost" value="1200" autocomplete="off" onchange="checkTotal()" />&nbsp;Mini Van (1,200 THB | 6 pax)</label><br />
<label for="xbed"><input type="checkbox" id="xbed" name="cost" value="1200" autocomplete="off" onchange="checkTotal()" />&nbsp;Extra Bed (1,200 THB)</label><br />
<input type="text" id="total" name="total" />

</form>
4

1 回答 1

0

document.getElementById http://www.w3schools.com/jsref/met_doc_getelementbyid.asp

于 2012-08-23T18:28:51.773 回答