2017-06-20 77 views
0

我想从html文本框中获取输入,并将输出排列在数组中。 我在这里跟着这个例子http://www.w3resource.com/javascript-exercises/javascript-array-exercise-13.php在Blank Array中添加项目,Javascript显示项目

现在我已经能够做这样的事情,除了我想它打印数组在div标签,我似乎得不到正确。代码如下所示

<!doctype html> 
<html> 
<head> 
<script type=text/javascript> 
var x; 
var y; 
var z; 
var array = Array(); 

function add_element_to_array() 
{ 
array[x] = document.getElementById("institution_name").value; 
array[y] = document.getElementById("degree_obtained").value; 
array[z] = document.getElementById("honors_acheieved").value; 
x++ 
y++ 
z++; 
document.getElementById("institution_name").value = ""; 
document.getElementById("degree_obtained").value = ""; 
document.getElementById("honors_acheieved").value = ""; 
} 

</script> 

</head> 
<title>Test Javascript</title> 
<h1>Test JS</h1> 
<body> 
<form name="xform" id="xform" method="post" action="samris.php"/> 
Institution Name: <input type="text" name="institution_name" id="institution_name" /> </br> 
Degree Obtained: <input type="text" name="degree_obtained" id="degree_obtained" /></br> 
Honors Achieved: <input type="text" name="honors_acheieved" id="honors_acheieved" /></br> 
</p> 
<input type="button" name="Add" id="add" value="add" onclick=add_element_to_array();/> 
</form> 

<div> 
</div> 


</body> 
</html> 

我不知道为什么它不打印出来回格列

+0

你给所有的文本字段相同的ID,它永远不会工作 – manny

+0

@曼尼,现在改变,仍然是一样的。我编辑过。并粘贴我拥有的。 – Pharell

回答

0

我相信,有2个问题 1.你不初始化数组索引(X,Y和Z)在你的JavaScript开始。请修改后检查下面

<script type=text/javascript> 
var x = 0; 
//var y; 
//var z; 
var array = Array(); 

function add_element_to_array() 
{ 
array[x++] = document.getElementById("institution_name").value; 
array[x++] = document.getElementById("degree_obtained").value; 
array[x++] = document.getElementById("honors_acheieved").value; 
//x++ 
//y++ 
//z++; 
document.getElementById("institution_name").value = ""; 
document.getElementById("degree_obtained").value = ""; 
document.getElementById("honors_acheieved").value = ""; 
} 

</script> 
  • 我看不出有任何的div在你的HTML你在哪里填充数组内容。您需要为div分配一个id,然后添加javascript以将内容分配给该div,如

    document.getElementById('arrayOutput')。value = array.toString();

  • 1

    <!doctype html> 
     
    <html> 
     
    
     
    <head> 
     
        <script type=text/javascript> 
     
        var array = []; 
     
    
     
        function add_element_to_array() { 
     
        \t array.push(document.getElementById("institution_name").value); 
     
        \t array.push(document.getElementById("degree_obtained").value); 
     
        \t array.push(document.getElementById("honors_acheieved").value); 
     
         
     
         document.getElementById("institution_name").value = ""; 
     
         document.getElementById("degree_obtained").value = ""; 
     
         document.getElementById("honors_acheieved").value = ""; 
     
        } 
     
        </script> 
     
    </head> 
     
    <title>Test Javascript</title> 
     
    <h1>Test JS</h1> 
     
    
     
    <body> 
     
        <form name="xform" id="xform" method="post" action="samris.php" /> Institution Name: 
     
        <input type="text" name="institution_name" id="institution_name" /> </br> 
     
        Degree Obtained: 
     
        <input type="text" name="degree_obtained" id="degree_obtained" /> 
     
        </br> 
     
        Honors Achieved: 
     
        <input type="text" name="honors_acheieved" id="honors_acheieved" /> 
     
        </br> 
     
        </p> 
     
        <input type="button" name="Add" id="add" value="add" onclick="add_element_to_array()" /> 
     
        </form> 
     
        <div> 
     
        </div> 
     
    </body> 
     
    
     
    </html>

    这将是代码。请检查这个,也许它可以帮助。你使用了根本不需要的x,y和z变量。只需推送数组中的值。

    代码中存在另一个错误。您将该数组声明为Array()。这是不正确的。在Javascript中,我们可以使用Array literal([])或new Array来声明一个数组。

    该数组将包含所有值。现在你可以打印一个div。