[英]Mad Libs Drop-down List Faliure
When I was making a Mad Libs game, I went to a part that was he/she. 当我制作Mad Libs游戏时,我去过的部分就是他/她。 But then the name was user-generated, so I can't choose either one.
但是后来这个名字是用户生成的,所以我不能选择其中一个。 So, I did this:
因此,我这样做:
var libButton = document.getElementById("lib-button"); var storyDiv = document.getElementById("story"); var teacher = document.getElementById("teacher"); var person = document.getElementById("person"); var gender = document.getElementById("gender").value; var libIt = function() { if (gender === "xxx") { heShe = "He/she"; } else if (gender === "male") { heShe = "He"; } else if (gender === "female") { heShe = "She"; } // The story! storyDiv.innerHTML = "Your story: At the beginning, " + person.value + " hated graphs. " + heShe + " just didn't get the hang of it. Then, " + teacher.value + " told " + person.value + " about the uses of graphs. " + person.value + " started to see how graphs are useful, therefore linking the outside life to graphs. Now, " + person.value + " knew why they are learning graphs, and started to get the hang of it. The end."; }; libButton.addEventListener('click', libIt);
<ul> <li>Teacher's Name: <input type="text" id="teacher"></li> <li>Someone's Name: <input type="text" id="person"></li> <li> <label>Gender of that person <select id="gender"> <option value = "xxx">---</option> <option value = "male">Male</option> <option value = "female">Female</option> </select> </label> </li> </ul> <button id="lib-button">Lib it!</button>
heShe
, even when the drop-down list is at "Male" or "Female".
heShe
。
I want it to be if the drop-down menu is at "Male", then it says He
, and when it's at "Female", It says She
, and when it's at "---", then it says He/she
.
He
,当位于“女性”时,说“ She
,当位于“ ---”时,则说“ He/she
。
Why doesn't that happen?
All of your getElementById
code is outside of libIt()
so it is grabbing all the values of the form when the page loads, essentially ignoring your choices. 您所有的
getElementById
代码都在libIt()
之外,因此它在页面加载时会获取表单的所有值,实际上忽略了您的选择。 For #story and #lib-button it doesn't matter, but #teacher, #gender, and #person need their values fetched at the top of the function, so you get the updated values at the time you clicked the button. 对于#story和#lib-button没关系,但是#teacher,#gender和#person需要在函数顶部获取它们的值,因此单击按钮时可以获取更新的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.