gpt4 book ai didi

Javascript 创建的表单未出现在页面上

转载 作者:行者123 更新时间:2023-12-02 16:30:15 25 4
gpt4 key购买 nike

我一直在调整我的剧本,看来我已经陷入了困境。我正在尝试学习如何使用 Javascript 生成表单,似乎每次我认为我已经掌握了它,我就会变得自信,添加更多内容,然后把事情搞砸。我无法在代码中发现问题。请提供任何帮助,我们将不胜感激。

Javascript:(form.js)

function initFirstLoad(){
var parentForm = document.getElementsByTagName('form')[0];
function addForm(){
var spawnForm = document.createElement("form");
spawnForm.setAttribute('name',"savefile");
spawnForm.setAttribute('method',"post");
spawnForm.setAttribute('action',"");
document.getElementsByTagName('body')[0].appendChild(spawnForm);
}
function addUniqueID(){
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var stringLength = 10;
var randomString = '';
for (var i=0; i<stringLength; i++) {
var randomNumber = Math.floor(Math.random() * characters.length);
randomString += characters.substring(randomNumber,randomNumber+1);
}
var uniqueID = document.createElement("input");
uniqueID.setAttribute('type',"text");
uniqueID.setAttribute('name',"filename");
uniqueID.setAttribute('readonly',"readonly");
uniqueID.setAttribute('value',randomString);
parentForm.appendChild(uniqueID);
}
function addWeaponField(){
var weapsName = document.createElement("input");
weapsName.setAttribute('type',"text");
weapsName.setAttribute('name',"textdata[]");

var weapsNameQt = document.createElement("input");
weapsNameQt.setAttribute('type',"number");
weapsNameQt.setAttribute('name',"textdata[]");
weapsNameQt.setAttribute('value',"0");

parentForm.appendChild(weapsName);
parentForm.appendChild(weapsNameQt);
}
function addAmmoField(){
var ammoName = document.createElement("input");
ammoName.setAttribute('type',"text");
ammoName.setAttribute('name',"textdata[]");

var ammoNameQt = document.createElement("input");
ammoNameQt.setAttribute('type',"number");
ammoNameQt.setAttribute('name',"textdata[]");
ammoNameQt.setAttribute('value',"0");

parentForm.appendChild(ammoName);
parentForm.appendChild(ammoNameQt);
}
function addSubmitButton(){
var weapsNameSubmit = document.createElement("input");
weapsNameSubmit.setAttribute('type',"submit");
weapsNameSubmit.setAttribute('name',"submitsave");
weapsNameSubmit.setAttribute('value',"Done!");
}
}
window.addEventListener("load", initFirstLoad);

HTML:(form.htm)

<!DOCTYPE HTML>
<html>
<body style="background-color: rgb(225,225,225)">
<h3>Javascript Created Form and Fields</h3>
<script type="text/javascript" src="form.js"></script>
</body>
</html>

我做错了什么吗?我的语法不正确吗?预先感谢您!

编辑:更正了addSubmitButton,它最初在我的代码中是正确的,但被意外删除。谢谢各位的帮助,问题解决了。我忘了将函数称为函数。

最佳答案

首先,您的代码结构非常奇怪(我认为)。如果您嵌套函数以将所有内容保持在一起,那么为什么不创建一个适当的基于原型(prototype)的“类”呢?这样你就可以使你的代码可重用。抛开这一点不谈,你有一个语法错误,并且你忘记调用你的函数。您还创建了一个提交按钮,但没有将其添加到表单中。事实上,您根本没有向 dom 添加表单。这是一个已解决问题的工作演示(但是必须以更好的方式构建此代码,但这取决于您:-):

working jsfiddle demo

function initFirstLoad(){
addForm();
var parentForm = document.getElementsByTagName('form')[0];
addUniqueID();
addWeaponField();
addAmmoField();
addSubmitButton();

function addForm(){
var spawnForm = document.createElement("form");
spawnForm.setAttribute('name',"savefile");
spawnForm.setAttribute('method',"post");
spawnForm.setAttribute('action',"");
document.getElementsByTagName('body')[0].appendChild(spawnForm);
}
function addUniqueID(){
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var stringLength = 10;
var randomString = '';
for (var i=0; i<stringLength; i++) {
var randomNumber = Math.floor(Math.random() * characters.length);
randomString += characters.substring(randomNumber,randomNumber+1);
}
var uniqueID = document.createElement("input");
uniqueID.setAttribute('type',"text");
uniqueID.setAttribute('name',"filename");
uniqueID.setAttribute('readonly',"readonly");
uniqueID.setAttribute('value',randomString);
parentForm.appendChild(uniqueID);
}
function addWeaponField(){
var weapsName = document.createElement("input");
weapsName.setAttribute('type',"text");
weapsName.setAttribute('name',"textdata[]");

var weapsNameQt = document.createElement("input");
weapsNameQt.setAttribute('type',"number");
weapsNameQt.setAttribute('name',"textdata[]");
weapsNameQt.setAttribute('value',"0");

parentForm.appendChild(weapsName);
parentForm.appendChild(weapsNameQt);
}
function addAmmoField(){
var ammoName = document.createElement("input");
ammoName.setAttribute('type',"text");
ammoName.setAttribute('name',"textdata[]");

var ammoNameQt = document.createElement("input");
ammoNameQt.setAttribute('type',"number");
ammoNameQt.setAttribute('name',"textdata[]");
ammoNameQt.setAttribute('value',"0");

parentForm.appendChild(ammoName);
parentForm.appendChild(ammoNameQt);
}
function addSubmitButton(){
var weapsNameSubmit = document.createElement("input");
weapsNameSubmit.setAttribute('type',"submit");
weapsNameSubmit.setAttribute('name',"submitsave");
weapsNameSubmit.setAttribute('value',"Done!");
parentForm.appendChild(weapsNameSubmit);
}
}
window.addEventListener("load", initFirstLoad());

关于Javascript 创建的表单未出现在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com