2013-11-09 108 views
-1

我想打一个脚本,我可以把我的表格的JavaScript阵列invoer[]并显示总我怎样才能把我的表单值的JavaScript数组中

它经常停止工作,我搜索了很多,我真的找不到正确的方法:d

这是我的javascript代码

var strijk = ['broek', 'hemd', 'tshirt', 'lakens', 'korte broek', 'babykledij']; 
var minuten = [5, 10, 5, 6, 3, 3]; 

function invoerstrijk() { 
    document.write("<form action='' method='get' name='strijkform'>"); 
    for (var a = 0; a < minuten.length; a++) { 
     document.write(strijk[a] + "<input id='" + strijk[a] + "' name ='" + strijk[a] + "' type='text' />" + "<BR>"); 
    } 

    document.write("<button onclick='opgeven()'>opgeven</button>"); 
    document.write("</form>"); 
} 

function opgeven() { 
    var invoer = []; 
    for (var a = 0; a < minuten.length; a++) { 
     invoer[a] = document.getElementByI(strijk[a]).value; 
    } 

    var totaal; 
    for (var a = 0; a < minuten.length; a++) { 
     totaal += parseint(invoer[a]) * parseint(minuten[a]); 
    } 
    document.write("<input name=" + strijk[a] + " type='text' value=" + invoer[a] + " readonly />"); 
    if (invoer != []) { 
     document.write("totaal aantal minuten" + totaal); 
    } else { 
     document.write("geen invoer"); 
    } 
} 

我的HTML看起来喜欢这个

<!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> 

<script type="text/javascript" > 
//my javasccript 
</script> 

<button id="B1" onclick="invoerstrijk()" >Nieuwe strijk</button> 
</body> 
</html> 
+1

你应该读好JavaScript的书,如果你是认真规划网络的东西。 –

+0

避免使用'document.write' – Oriol

+0

这是学校,我正在学习它。我知道一些,但没有那么多,你们都在帮助我! TY! –

回答

0

这应该工作:

var strijk = ['broek', 'hemd', 'tshirt', 'lakens', 'korte broek', 'babykledij']; 
var minuten = [5, 10, 5, 6, 3, 3]; 

function invoerstrijk() { 
    document.write("<form action='' method='get' name='strijkform' onsubmit='return false;'>"); 
    for (var a = 0; a < minuten.length; a++) { 
    document.write(strijk[a] + "<input id='" + strijk[a] + "' name ='" + strijk[a] + "' type='text' />" + "<BR>"); 
    } 

    document.write("<button onclick='opgeven()'>opgeven</button>"); 
    document.write("</form>"); 
} 

function opgeven() { 
    var invoer = []; 
    for (var a = 0; a < minuten.length; a++) { 
    invoer.push(document.getElementById(strijk[a]).value); 
    } 

    var totaal = 0; 
    for (var a = 0; a < minuten.length; a++) { 
    totaal += (parseInt(invoer[a]) * parseInt(minuten[a])); 
    } 
    // !!! this is wrong because it is out of for !!! 
    //document.write("<input name=" + strijk[a] + " type='text' value=" + invoer[a] + " readonly />"); 
    if (invoer.length > 0) { 
    document.write("totaal aantal minuten " + totaal); 
    } else { 
    document.write("geen invoer"); 
    } 
} 

一些解释:

  1. 的onsubmit = '返回false;' - 不发送表单并重新加载页面。
  2. invoer.push(document.getElementById(strijk [a]).value); - 将元素放入数组使用推送。
  3. var totaal = 0; - 初始化变量0,因为它是未定义的。
  4. 拼写错误:不好:parseint,好:parseInt;坏:getElementByI,好:的getElementById
  5. 与只读输入该生产线是错误的,因为它是出了这么一个变量没有定义。

Plunker example

+0

感谢您的解释! –