javascript - 在空白数组中添加项目,Javascript显示项目

标签 javascript php

我想从 html 文本框中获取输入并将输出排列在数组中。 我在这里遵循这个例子 http://www.w3resource.com/javascript-exercises/javascript-array-exercise-13.php

现在我已经能够做这样的事情了,除了我希望它在 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>

我想知道为什么它不将其打印回 div 列

最佳答案

<!doctype html>
<html>

<head>
    <script type=text/javascript>
    var array = [];

    function add_element_to_array() {
    	array.push(document.getElementById("institution_name").value);
    	array.push(document.getElementById("degree_obtained").value);
    	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 中,我们可以使用数组文字 ([]) 或 new Array 来声明数组。

该数组将包含所有值。现在您可以在 div 中打印它。

关于javascript - 在空白数组中添加项目,Javascript显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652876/

相关文章:

javascript - 隐藏列时的html colspan问题

javascript - 这个javascript代码函数是什么意思

php - 如何根据表单提交的日期搜索记录

php - Laravel:表单模型绑定(bind)和资源 Controller 错误

PHP 显示来自 MySQL 的图像 BLOB

javascript - 如何在 PHP 的 HTML 页面中动态更改登录和注销链接?

javascript - 删除 :valid pseudo class from inputs with Javsacript

javascript - 单击 li 时关闭菜单

javascript - 如何在javascript中将数组转换为没有逗号和空格分隔的字符串而不连接?

php - 对查询中的值求和并将其显示在按项目 ID 过滤的表中