2010-10-06 53 views
0

我发现这个代码在这个网站的其他地方。我正在调整它,但由于我是Javascript新手,我不知道如何让总和出现在你点击某种“计算”按钮之后。我试图自己做,但数字自动计算,我需要一个解决方案,以便它不这样做。如何通过提交按钮在javascript中的广播选项

<script type="text/javascript"> 
    function DisplayPrice(price){ 
        var val1 = 0; 
        for(i = 0; i < document.form1.price.length; i++){ 
          if(document.form1.price[i].checked == true){ 
            val1 = document.form1.price[i].value; 
          } 
        } 

        var val2 = 0; 
        for(i = 0; i < document.form2.price2.length; i++){ 
          if(document.form2.price2[i].checked == true){ 
            val2 = document.form2.price2[i].value; 
          } 
        } 

        var sum=parseInt(val1) + parseInt(val2); 
     document.getElementById('totalSum').value=sum; 
    } 
</script> 

选择一个号码:



Choose another number:<br> 
<form name="form2" id="form2" runat="server"> 
    <br> 
    <input id="rdo_1" type="radio" value="345" name="price2" onclick="DisplayPrice(this.value);">345 
    <br> 
    <input id="rdo_2" type="radio" value="87" name="price2" onclick="DisplayPrice(this.value);">87 
    <br> 
</form> 

回答

0

Put onclick =“DisplayPrice();”在另一个计算按钮(输入类型按钮或提交)中,从两个单选按钮中删除onclicks,并修改DisplayPrice以汇总表单元素的值。

+0

“修改DisplayPrice以汇总表单元素的值。”你能告诉我怎么做这部分? – Lola 2010-10-07 14:35:53

+0

基本上类似于document.getElementById('totalSum')。value = parseInt(document.getElementById('rdo_1')。value)+ parseInt(document.getElementById('rdo_2')value) – Detect 2010-10-07 17:14:35

+0

好吧,我明白了。你给我的代码没有工作。它不断添加表单1和表单2的全部总和,所以我使用它来代替(以防将来帮助其他人): var sum = parseInt(val1)+ parseInt(val2); document.getElementById('totalSum')。value = sum; }然后对于按钮它是: 谢谢! – Lola 2010-10-07 19:12:40

相关问题