使用Javascript动态数组输入
Dynamically array input with Javascript
我想输入数组的数量,输出将跟随它的数量。例如:如果我在输入文本中输入"7"。结果将显示多达7。这是我的代码:
<html>
<head>
<title>JavaScript - Input Text Field</title>
</head>
<body>
<form name="test">
<H2>Enter something into the field and press the button. <br></H2>
<P>Amount of Tables: <input type="TEXT" name="amount"><BR><BR>
<input type="Button" Value="Show and Clear Input" onClick="myFunction()"></P>
</form>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i;
var j = document.getElementsByName("amount");
for (i = 0; i < j.length; i++) {
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
您的JavaScript 有问题
参见代码:
function myFunction() {
var text = "";
var i;
var j = document.getElementsByName("amount")[0];
for (i = 0; i < j.value; i++) {
text += "The number is " + j.value + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
.getElementsByName
返回一个元素数组,因此需要指定元素的索引,以便访问其属性。
Fiddle here
相关文章:
- $q.all当输入数组中的一项不是promise时,Typescript检查器失败
- 你能提交一个带有文本输入数组的表单吗
- 使用 jQuery 和 Ajax 发送输入数组
- $.when.apply 根据输入数组的长度返回具有不同结构的结果
- PHP只读取动态创建的输入数组的第一个值
- 使用jquery设置输入数组值
- 使用 javascript 验证输入数组
- 角度 UI 预测文本:如何输入未包含在输入数组中的数据
- 将动态输入索引到其他动态输入数组
- 循环遍历输入数组以设置值,“无法设置未定义的属性值”
- 在输入数组元素时在 html 页面中显示数组元素
- Javascript 隐藏输入数组
- 编码然后在 base64 中解码不会输出我的输入数组缓冲区
- 将隐藏的输入数组传递给 jQuery load()
- 将输入数组中的特定输入值发送到 JavaScript
- 在输入数组的 split() 和子字符串 () 之后的文本区域中显示
- Jquery 添加未传递给 php 的输入数组字段
- Javascript函数显示用户输入数组
- 从输入数组中获取sum's元素
- 如何在文本输入数组中插入字符串