[英]why are names not being added to the list?
我找到了这个小提琴,我正试图让它工作......我无法弄清楚为什么这些名字没有被添加到列表中,由于某种原因,添加按钮就像一个提交按钮,我不知道为什么。 ..它应该将所有数字添加到列表中,因此当我单击提交时,它应该将数字发送到数组中..
JavaScript:
function bindName() {
var inputNames = document.getElementById("names").getElementsByTagName("inputNames");
for (i = 0; i < inputNames.length; i++) {
inputNames[i].onkeydown = function() {
if (this.value == "") {
setTimeout(deletename(this), 1000);
}
}
}
}
document.getElementById("addName").onclick = function() {
var num1 = document.getElementById("name");
var myRegEx = /^[0-9]{10}$/;
var myRegEx = /^[0-9]{10}$/;
var itemsToTest = num1.value;
if (myRegEx.test(itemsToTest)) {
var form1 = document.getElementById("names");
var nameOfnames = form1.getElementsByTagName("inputNames").length;
var newGuy1 = document.createElement("inputNames");
newGuy1.setAttribute("id", nameOfnames);
newGuy1.setAttribute("type", "text");
newGuy1.setAttribute("value", num1.value);
form1.appendChild(newGuy1);
num1.value = "";
bindName();
}
else {
alert('error');
}
};
HTML:
<h1>Enter Name</h1>
<div id="mainName">
<h2>name</h2>
<label for="name">Add Names: </label>
<input id="name" type="text">
<button id="addName">Add</button>
<form>
<div id="names">
</div>
<input METHOD="POST" action="text.php" type="submit" value="Submit">
</form>
</div>
因为这个/^[0-9]{10}$/;
将只接受 10 个数字,并且仅此,尝试输入1234567890
,您将不会看到任何错误。
我见过
document.createElement("inputNames");
不应该
document.createElement("input");
?
我不确定为什么您的“姓名”字段仅限于 10 位数字,但我已经开始工作了。 http://jsfiddle.net/y8Uju/4/
我认为问题在于您试图创建一个带有标签名称 inputNames 的元素,但这不是一个有效的标签。 相反,我将其更改为创建输入,并将 class 设置为 inputNames。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.