Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环不会累计窗体值的运行总数_Javascript_Html - Fatal编程技术网

Javascript 循环不会累计窗体值的运行总数

Javascript 循环不会累计窗体值的运行总数,javascript,html,Javascript,Html,我是Javascript新手,正在做一个在线练习。 问题是循环没有从表单中提取值,因此运行计数永远不会更新。感谢您的帮助 实际上还有一个问题,您知道为什么没有从选中的输入框中提取值吗?即使我在循环中迭代,函数也返回零 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmln

我是Javascript新手,正在做一个在线练习。 问题是循环没有从表单中提取值,因此运行计数永远不会更新。感谢您的帮助

实际上还有一个问题,您知道为什么没有从选中的输入框中提取值吗?即使我在循环中迭代,函数也返回零

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>
<body>
<h1>Configure Your GT Super Sportscar</h1>
<form id="orderform" action="#">
<table border="1">

<tr>

<td><input type="radio" name="manual" checked="checked" id="manual" value="17790.00" />GT Manual</td><td>$17,790.00</td> 
</tr>
<tr>
<td><input type="radio" name="manual" id="auto" value="18590.00" />GT Automatic</td><td>$18,590.00</td>
</tr>
<tr>
<td><input type="radio" name="manual" id="smanual" value="22455.00" />GT-S Manual</td><td>$22,455.00</td>
</tr>
<tr>
<td><input type="radio" name="manual" id="sshift" value="23155.00"/>GT-S Sportshift</td><td>$23,155.00</td>
</tr>
</table>
<table border="1">

<tr>
<td><input type="radio" name="manual" id="combo1" value="1235.00" />Option Combo #1</td><td>$1235.00</td>
</tr>
<tr>
<td><input type="radio" name="manual" id="combo2" value="3354.00" />Option Combo #2
<ul>
<li>Rear Spoiler and Fog Lamps</li>
<li>Keyless Entry</li>
<li>Power Tint and Side Moonroof</li>
<li>Power Windows, Doors, and Cruise Control</li>
</ul>
</td>
<td>$3354.00</td>
</tr>
<tr>
<td><input type="radio" name="manual" id="nocombo" value="0" />No Combo</td><td>$0</td>
</tr>

</table>
<table border="1">

<tr>

<td><input type="checkbox" name="amen" id="cac" value="550.00"/>CD Autochanger</td><td>$550.00</td> 
</tr>
<tr>
<td><input type="checkbox" name="amen" id="vss" value="399.00"/>VIP Security System</td><td>$399.00</td>
</tr>
<tr>
<td><input type="checkbox" name="amen" id="adm" value="295.00"/>Auto Dimming Mirror</td><td>$295.00</td>
</tr>

</table>
<table border="1">

<tr>
<td><input type="text" name="price" id="price" /></td><td><input type="button" onclick="showit()" value="Calculate Total" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
/**
* @author Isaac's
*/
function getval(){
var options = document.forms["orderform"].manual;
var optionslength = options.length;
var total = 0;
for (var i = 0; i &lt; optionslength; i++) {
if (options[i].checked) {
options[i].value += total;
}
return total;
}
}
var result1 = getval();
function getval2(){
var total=0;
var checkboxes = document.forms["orderform"].amen;
for (var i = 0; i &lt; checkboxes.length; i++) {
checkboxes[i].value += total;
}
return total;
}

var result2 = getval2();

function showit(){
var total = parseFloat(result1) + parseFloat(result2)
alert(total);
}

</script> 
</body>
</html>

无标题文件
配置您的GT超级跑车
燃气轮机手册$17790.00
GT Automatic$18590.00
GT-S手册$22455.00
GT-S Sportshift$23155.00
选项组合#1$1235.00
选项组合#2
  • 后扰流板和雾灯
  • 无钥匙进入
  • 动力着色和侧天窗
  • 电动车窗、车门和巡航控制
$3354.00 无组合$0 CD自动换碟机550.00美元 VIP安全系统399.00美元 自动调光后视镜295.00美元 /** *@作者艾萨克 */ 函数getval(){ var options=document.forms[“orderform”].manual; var optionslength=options.length; var合计=0; 对于(变量i=0;i选项长度;i++){ 如果(选项[i]。选中){ 选项[i]。值+=总计; } 返回总数; } } var result1=getval(); 函数getval2(){ var合计=0; var复选框=document.forms[“orderform”].amen; 对于(变量i=0;i复选框.length;i++){ 复选框[i]。值+=总计; } 返回总数; } var result2=getval2(); 函数showit(){ var total=parseFloat(结果1)+parseFloat(结果2) 警报(总数); }
在这段代码中,你永远不会对total做任何事情,它总是0

function getval2(){
var total=0;
var checkboxes = document.forms["orderform"].amen;
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].value += total;
}
return total;
}
函数getval2(){ var合计=0; var复选框=document.forms[“orderform”].amen; 对于(变量i=0;i
另外,有一个技巧可以帮助您调试这些类型的东西(并使代码总体上更好),那就是尝试使用。

代码有多个问题

这是一个你可以玩的游戏

有4个大问题使代码无法工作:

  • 在if循环中,当您要添加到您写入的总数时:

    options[i].value += total;
    
    这将作用于
    选项[i].value
    。您想更改
    总数
    ,因此应该编写

    total += parseFloat(options[i].value);
    
  • getval()

    function getval(){
        var options = document.forms["orderform"].manual;
        var optionslength = options.length;
        var total = 0;
        for (var i = 0; i &lt; optionslength; i++) {
            if (options[i].checked) {
                options[i].value += total;
            }
            return total; // <=======   THIS IS STILL INSIDE THE FOR LOOP!!!!!!!!
        }
    }
    
    function getval(){
        var options = document.forms["orderform"].manual;
        var optionslength = options.length;
        var total = 0;
        for (var i = 0; i &lt; optionslength; i++) {
            if (options[i].checked) {
                total += parseFloat(options[i].value);
            }            
        }
        return total; // <===== ALL CALCULATIONS ARE COMPLETED, SO WE CAN RETURN.
    }
    
    函数getval(){ var options=document.forms[“orderform”].manual; var optionslength=options.length; var合计=0; 对于(变量i=0;i选项长度;i++){ 如果(选项[i]。选中){ 选项[i]。值+=总计; }
    return total;//你也可以发布你的html吗?html会自动呈现,我怎么只显示代码?缩进代码四个空格(
    101010
    按钮).Markdown提示:谢谢你的回答。它被缩进了,但是格式丢失了。不管怎样,你说我对total变量什么都不做,那么脚本在做什么呢?特别是在这一行:复选框[I].value+=total;如果有任何帮助,我使用此代码的目的是循环并获取所选收音机和复选框的值。@Isaac-您声明并初始化total为0。然后循环检查复选框并将total添加到复选框[i].value。但是total始终为0,因为您在任何地方都不会更改它的值。那么,您在这里的目的是什么?如果您试图获取复选框的值,那么为什么要将total添加到它?只需获取值。我的想法是,因为我有两组广播框,所以我会循环遍历它们,因为这两组都有不同的名称我只想把它们(各自的值)加在一起。Total充当表单中所有选定值的占位符。