gpt4 book ai didi

javascript - 帮助我正在开发的 Javascript 程序

转载 作者:行者123 更新时间:2023-11-30 18:44:34 25 4
gpt4 key购买 nike

我正在完成一项作业,遇到了一个对我来说很奇怪的问题,我不知道如何解决。这很难解释,但希望我能解释得足够好,让别人能理解。我正在使用 HTML 表单(单选按钮、复选框、选择框、文本框),我的老师给了我们一些任务,让我们用 JavaScript 处理 HTML。我一直在完成这些任务,但在某一点之后,当我尝试声明一个对象并从表单中提取一些 HTML 时,它说它是“空的”。但是,如果我将它放在页面顶部,它会起作用,但随后它会取消它下面的任何内容。 (我知道这听起来令人困惑,但也许看到一些代码可能会有所帮助......)

   <html>
<body>
<script language="javascript">
<!--
function fred ()
{


option1=document.f1.zooanimal.option1

if(document.f1.game1.checked||document.f1.game2.checked||document.f1.game3.checked||document.f1.game4.checked)
{
return true
}
else
{
alert("Must Select at least ONE Checkbox Value!!!")
}


if (document.f1.zooanimal.selectedIndex=option1);
{
alert("Must Select Option other than default value!");
}



bigtextstr=document.f1.bigtext.value
bigedit=bigtextstr.replace(/ /g,"+")
bigedit2=bigedit.replace(/[\r\n]/g , "")

document.write("Characters in Text Area Before Edits="+"<br>")
biglen=bigtextstr.length
document.write(biglen+"<br>")

document.write("Characters in Text Area After Edits="+"<br>")
newbiglen=bigedit2.length
document.write(newbiglen+"<br>")
}


-->
</script>
<p>
<form name="f1">
<br>
Name <input type="text" name="nametext" size="30" value="First Last"><p>
List your favorite things to do <P><textarea name="bigtext" rows="5" cols="40">default value</textarea>

<p>What is your favorite animal to see at the zoo?

<select name="zooanimal">
<option name= "option1" selected="yes">default value
<option name="option2">elephants
<option name="option3">giraffes
<option name="option4">tigers
<option name="option5">seals
</select>

<p>
What is your favorite color?<br><p>
blue <input name="rb" type="radio" value="blue" checked> green <input name="rb" type="radio" value="green">
pink <input name="rb" type="radio" value="pink"> yellow <input name="rb" type="radio" value="yellow"> red <input name="rb" type="radio" value="red"> black <input name="rb" type="radio" value="black"></p>

Which of these games do you play?<br><p>
Starcraft <input name="game1" value="Starcraft" type="checkbox"> World of Warcraft <input name="game2" value="WorldofWarcraft" type="checkbox">
League of Legends <input name="game3" value="LeagueofLegends" type="checkbox"> none <input name="game4" value="none"
type="checkbox"><P>



<p><input type="button" value="EDIT AND REPORT" onClick="fred()">


<p>

<p>
</form>
</body>
</html>

现在查看脚本部分,直到那时一切正常,但如果我尝试在此之后添加任何其他表单引用,它会说它为空。我很困惑,已经为此工作了几个小时。有人可以帮忙吗?

最佳答案

当引用[单个] 对象时,您应该使用 getElementById('elementid') 获取它们或 getElementsByName('elementname')[0] ,而不是试图通过父“节点”深入研究所需的元素。

同时将 JavaScript 放在文档的头部或正文的底部。这些是不应运行 onload 的脚本的最佳位置;)

此外,请注意“document.write”将覆盖整个文档。你应该放一个 <p>在你的页面中,给它一个唯一的 id...

<p id="unique_id_alerts"></p>

然后像这样写入:

document.getElementById('unique_id_alerts').innerHTML = 'Output goes here.';

关于javascript - 帮助我正在开发的 Javascript 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5847880/

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