gpt4 book ai didi

JavaScript 不执行 HTML 中的文本框值

转载 作者:行者123 更新时间:2023-11-27 23:54:49 25 4
gpt4 key购买 nike

我对 HTML 中的 JavaScript 很陌生,并得到了使用 JS 编写 Mad Lib 表单的项目。我对它进行了编码并检查了很多次,但我找不到代码的问题:程序没有更改HTML文档中的文本框值。你能帮忙吗?代码:

function madLib() {
var name, object, objectTwo, adjective, adverb, verb, place, mood, buildingStructure;
name = document.MadLibForm.name.value;
object = document.MadLibForm.object.value;
objectTwo = document.MadLibForm.objectwo.value;
adjective = document.MadLibForm.adjective.value;
adverb = document.MadLibForm.adverb.value;
verb = document.MadLibForm.verb.value;
place = document.MadLibForm.place.value;
mood = document.MadLibForm.mood.value;

buildingStructure = name + " went to PLACE after a long time. " + name + " loved " + place + " because it was his/her favorite place when she was a kid. " + name + " took the " + adjective + " " + obeject + " with him to the " + place + ", which he/she used to it " + adverb + verb + ". He/She also took " + objectTwo + " with him/her, but did not use it at all during the visit. Still, at the end of the visit, he/she was very " + mood + " .";

document.MadLibForm.displayFullMadLib.value = buildingStructure;
}
<!DOCTYPE html>

<head>
<title>Mad Libs</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css" type="text/css">
<script>
function madLib() {
var name, object, objectTwo, adjective, adverb, verb, place, mood, buildingStructure;
name = document.MadLibForm.name.value;
object = document.MadLibForm.object.value;
objectTwo = document.MadLibForm.objectwo.value;
adjective = document.MadLibForm.adjective.value;
adverb = document.MadLibForm.adverb.value;
verb = document.MadLibForm.verb.value;
place = document.MadLibForm.place.value;
mood = document.MadLibForm.mood.value;

buildingStructure = name + " went to PLACE after a long time. " + name + " loved " + place + " because it was his/her favorite place when she was a kid. " + name + " took the " + adjective + " " + obeject + " with him to the " + place + ", which he/she used to it " + adverb + verb + ". He/She also took " + objectTwo + " with him/her, but did not use it at all during the visit. Still, at the end of the visit, he/she was very " + mood + " .";

document.MadLibForm.displayFullMadLib.value = buildingStructure;
}
</script>
</head>

<body>
<div id="main">
<form name="MadLibForm">
<table>
<tr>
<th colspan="2">wEiRd Mad Lib</th>
</tr>
<tr>
<td>Your Name</td>
<td>
<input type="text" name="name" class="inputs">
</td>
</tr>
<tr>
<td>Object</td>
<td>
<input type="text" name="object" class="inputs">
</td>
</tr>
<tr>
<td>Another Object</td>
<td>
<input type="text" name="objecttwo" class="inputs">
</td>
</tr>
<tr>
<td>Place</td>
<td>
<input type="text" name="place" class="inputs">
</td>
</tr>
<tr>
<td>Verb</td>
<td>
<input type="text" name="verb" class="inputs">
</td>
</tr>
<tr>
<td>Adjective</td>
<td>
<input type="text" name="adjective" class="inputs">
</td>
</tr>
<tr>
<td>Adverb</td>
<td>
<input type="text" name="adverb" class="inputs">
</td>
</tr>
<tr>
<td>Mood</td>
<td>
<input type="text" name="mood" class="inputs">
</td>
</tr>
<tr>
<td>
<div>
<input type="button" onClick="madLib()" value="Get Your Story!" id="button">
</div>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;
<textarea name="displayFullMadLib" rows="9" cols="65"></textarea>
</td>
</tr>
</table>
</form>
</div>
<!--main-->
</body>

</html>

非常感谢:)

最佳答案

<强> Your updated fiddle 两次更改后工作正常。

  1. 更改:

    objectTwo = document.MadLibForm.objectwo.value;

    致:

    objectTwo = document.MadLibForm.objecttwo.value; //two `t`
  2. object 替换为 object:

    ..." took the " + adjective + " " + object + " with him to the " + place ...+ 

JS:

madLib = function() {
var name, object, objectTwo, adjective, adverb, verb, place, mood, buildingStructure;
name = document.MadLibForm.name.value;
object = document.MadLibForm.object.value;
objectTwo = document.MadLibForm.objecttwo.value;
adjective = document.MadLibForm.adjective.value;
adverb = document.MadLibForm.adverb.value;
verb = document.MadLibForm.verb.value;
place = document.MadLibForm.place.value;
mood = document.MadLibForm.mood.value;

buildingStructure = name + " went to PLACE after a long time. " + name + " loved " + place + " because it was his/her favorite place when she was a kid. " + name + " took the " + adjective + " " + object + " with him to the " + place + ", which he/she used to it " + adverb + verb + ". He/She also took " + objectTwo + " with him/her, but did not use it at all during the visit. Still, at the end of the visit, he/she was very " + mood + " .";

document.MadLibForm.displayFullMadLib.value = buildingStructure;
}

关于JavaScript 不执行 HTML 中的文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363971/

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