我試着寫一個函數,該函數從用戶接收關於要在表單中創建多少個字段的輸入。然後該函數創建一個包含數字(等同於用戶輸入)文本框和一個提交按鈕的表單。我寫了函數,但它似乎沒有工作,我哪裏出錯了。如果有人能幫助我,我將不勝感激。 我的代碼:使用Javascript動態創建表單
function createTextBox()
{
var box=document.getElementById('pop'); //getting the ID of the containner
var val=document.getElementById('uValue').value; //getting the Input value from the user
var candidateForm=document.createElement('form'); //Creating a form and giving the attributes
candidateForm.name="candidateForm";
candidateForm.method="post";
candidateFomr.action="process.php";
for(var i=0; i<val;i++)
{
var newTextbox = document.createElement('input'); //creating the textboxes according to the users input
newTextbox.type="textbox";
newTextbox.name="candidate[]";
newTextbox.id="candidateId";
candidateForm.appendChild(newTextbox); //putting the created textboxes in the form
}
var saveButton=document.createElement('input'); //creating the submit button which when clicked will save the value written in the textboxes
saveButton.type="submit";
saveButton.name="submitButton";
saveButton.value="Enter";
candidateForm.appendChild(saveButton); //putting the submit button in the form
box.appendChild(candiateForm); //And putting the form in the containner.
//alert (val);
}
這裏是HTML部分提前
<body>
<input type="textbox" name="value_box" id="uValue" ></input>
<input type="button" onclick="javascript:createTextBox()" value="Click"></input>
<div id="pop"></div>
</body>
感謝:d
當我運行它,它不工作,我不知道爲什麼它不工作。 :( – Chakra 2012-07-22 17:40:45
**如何**它不工作?會發生什麼? – SLaks 2012-07-22 17:41:02
你確定val是一個數字嗎?也許它是一個像「5」而不是5的字符串。嘗試var val = + document.getElementById('uValue' ).value – Azder 2012-07-22 17:42:04