我是 javascript 新手。我有一個 JavaScript 程式,它根據用戶需要添加欄位,但 input2 沒有正確顯示給用戶輸入并帶有說明。請運行代碼 belo 來查看。
<html>
<head>
<script type='text/javascript'>
function addFields(){
// Number of inputs to create
var number = document.getElementById("member").value;
// Container <div> where dynamic content will be placed
var container = document.getElementById("container");
// Clear previous contents of the container
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i=0;i<number;i ){
// Append a node with a random text
container.appendChild(document.createTextNode("Nome " (i 1)));
var input = document.createElement("input");
input.type = "text";
input.name = "Nome etapa" i;
input.style.border = "1px solid black";
container.appendChild(input);
container.appendChild(document.createElement("br"));
container.appendChild(document.createTextNode("Descri??o " (i 1)));
var input2 = document.createElement("input2");
input2.type = "text";
input2.name = "Descri??o etapa" i;
input2.style.border = "thin dotted red";
container.appendChild(input2);
container.appendChild(document.createElement("br"));
}
}
</script>
</head>
<body>
<input type="text" id="member" name="member" value="">Number of members: (max. 10)<br />
<a href="#" id="filldetails" onclick="addFields()">Fill Details</a>
<div id="container"/></div>
</body>
</html>
是代碼有問題還是瀏覽器有問題?或者 javascript 中的 filds 數量有一些限制?如何使 'Descri??o' 欄位 apeear?
關于可能是什么錯誤的任何建議?
uj5u.com熱心網友回復:
你沒有結束</p>和</div>標簽。
它們不是自閉合標簽。
編輯:
在回答您的編輯,你需要改變value=""的<input>,以value=1
改變 var input2 = document.createElement("input");
uj5u.com熱心網友回復:
使用下面的代碼。我將 document.createElement("input2") 更改為 document.createElement("input")
<html>
<head>
<script type='text/javascript'>
function addFields(){
// Number of inputs to create
var number = document.getElementById("member").value;
// Container <div> where dynamic content will be placed
var container = document.getElementById("container");
// Clear previous contents of the container
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i=0;i<number;i ){
// Append a node with a random text
container.appendChild(document.createTextNode("Nome " (i 1)));
var input = document.createElement("input");
input.type = "text";
input.name = "Nome etapa" i;
input.style.border = "1px solid black";
container.appendChild(input);
container.appendChild(document.createElement("br"));
container.appendChild(document.createTextNode("Descri??o " (i 1)));
var input2 = document.createElement("input");
input2.type = "text";
input2.name = "Descri??o etapa" i;
input2.style.border = "thin dotted red";
container.appendChild(input2);
container.appendChild(document.createElement("br"));
}
}
</script>
</head>
<body>
<input type="text" id="member" name="member" value=2>Number of members: (max. 10)<br />
<a href="#" id="filldetails" onclick="addFields()">Fill Details</a>
<div id="container"/></div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/362457.html
標籤:javascript
