[英]How can I change the HTML of an element when different options are selected in a separate <select> element?
So on my website, I'm trying to make it so that when you select a certain Course Name using the <select>
element, you also change The HTML of the Course Description directly below it so that the Description corresponds with the course. 因此,在我的网站上,我试图做到这一点,以便当您使用
<select>
元素选择某个课程名称时,您还可以直接更改其下方的“课程描述”的HTML,以使“说明”与课程相对应。 Here's what I've got so far: 到目前为止,这是我得到的:
< script > function myFunctionHTML() { document.getElementById('Course_Description').innerHTML = "I like coding" }; function myFunctionComp() { document.getElementById('Course_Description').innerHTML = "I like computers" }; var H = document.getElementById('Course_HTML').innerHTML; var C = document.getElementById('Course_Comp').innerHTML; var Course_Select = document.getElementById('Course_Name').innerHTML; function myFunctionVar() { if Course_Select = H { myFunctionHTML(); } if Course_Select = C { myFunctionComp(); } }; < /script>
<label class="w3-label w3-text-black">Course Name</label> <select id="Course_Name" name="Course_Name" onchange="myFunctionVar()" style="width:95%;" required> <option value="IntroHTML" id="Course_HTML">Intro to HTML and CSS</option> <option value="BuildComputers" id="Course_Comp">Build A Computer</option> </select> <label class="w3-label w3-text-black">Course Description</label> <p id="Course_Description">Hi there</p>
I'm pretty sure the myFunctionVar() is just wrong, but after researching on other websites and other questions I can't seem to find anything. 我很确定myFunctionVar()错了,但是在研究其他网站和其他问题之后,我似乎什么也没找到。
No need write that much of code to just change innerHTML
of an element by change in select
options. 无需编写大量代码即可通过更改
select
选项来更改元素的innerHTML
。
You can refactor your code to be very simple and more readable and even extendable for future options
on select. 您可以将代码重构为非常简单,可读性更好,甚至可扩展的代码,以供将来
options
使用。
var htmlDesc = "I like coding"; var compDesc = "I like computers"; //keep more course descriptions here function changeDescription(e) { var desc = compDesc; if(e.value === "IntroHTML") desc = htmlDesc; //default HTML description //keep more conditions to check for course selected document.getElementById('Course_Description').innerHTML = desc; //changing innerHTML of description holder };
<label class="w3-label w3-text-black">Course Name</label> <select id="Course_Name" name="Course_Name" onchange="changeDescription(this);" style="width:95%;" required> <option value="IntroHTML" id="Course_HTML">Intro to HTML and CSS</option> <option value="BuildComputers" id="Course_Comp">Build A Computer</option> </select> <label class="w3-label w3-text-black">Course Description</label> <p id="Course_Description">I like coding</p>
First of all, Remove ";" 首先,删除“;” from functions.
从功能。 Then, you should get the value attribute of option and not innerHTML.
然后,您应该获得option的value属性,而不是innerHTML。
First get the value of selected option and then change the description 首先获取所选选项的值,然后更改描述
var e = document.getElementById(selectId);
var option = e.options[e.selectedIndex].value;
function myFunctionHTML() { document.getElementById('Course_Description').innerHTML = "I like coding" } function myFunctionComp() { document.getElementById('Course_Description').innerHTML = "I like computers" } var H = document.getElementById('Course_HTML').innerHTML; var C = document.getElementById('Course_Comp').innerHTML; var Course_Select = document.getElementById('Course_Name').innerHTML; function myFunctionVar(selectId) { var e = document.getElementById(selectId); var option = e.options[e.selectedIndex].value; if (option === "IntroHTML") { myFunctionHTML(); } if (option === "BuildComputers") { myFunctionComp(); } }
<label class="w3-label w3-text-black">Course Name</label> <select id="Course_Name" name="Course_Name" onchange="myFunctionVar('Course_Name')" style="width:95%;" required> <option value="IntroHTML" id="Course_HTML">Intro to HTML and CSS</option> <option value="BuildComputers" id="Course_Comp">Build A Computer</option> </select> <label class="w3-label w3-text-black">Course Description</label> <p id="Course_Description">Hi there</p>
here is the error free code of yours try to replace it, with your code and test is works as you expected. 这是您尝试用它替换的无错误代码,并且您的代码可以按预期工作。 removed
;
删除
;
from you code corrected syntax error in `if condition 来自您的代码,如果条件已更正,语法错误
function myFunctionHTML() {
document.getElementById('Course_Description').innerHTML = "I like coding";
}
function myFunctionComp() {
document.getElementById('Course_Description').innerHTML = "I like computers";
}
var H = document.getElementById('Course_HTML').innerHTML;
var C = document.getElementById('Course_Comp').innerHTML;
var Course_Select = document.getElementById('Course_Name').innerHTML;
function myFunctionVar() {
if (Course_Select == 'H') {
myFunctionHTML();
}
if (Course_Select == 'C') {
myFunctionComp();
}
}
Here is the full code 这是完整的代码
<label class="w3-label w3-text-black">Course Name</label>
<select id="Course_Name" name="Course_Name" onchange="myFunctionVar()" style="width:95%;" required>
<option value="IntroHTML" id="Course_HTML">Intro to HTML and CSS</option>
<option value="BuildComputers" id="Course_Comp">Build A Computer</option>
</select>
<label class="w3-label w3-text-black">Course Description</label>
<p id="Course_Description">Hi there</p>
<script>
function myFunctionHTML() {
document.getElementById('Course_Description').innerHTML = "I like coding"
}
function myFunctionComp() {
document.getElementById('Course_Description').innerHTML = "I like computers"
}
function myFunctionVar() {
var H = document.getElementById('Course_HTML').innerHTML
var C = document.getElementById('Course_Comp').innerHTML
var e = document.getElementById("Course_Name");
var Course_Select = e.options[e.selectedIndex].value;
if (Course_Select == 'IntroHTML')
{
myFunctionHTML()
}
else if (Course_Select == 'BuildComputers')
{
myFunctionComp()
}
}
</script>
Here is the fiddle 这是小提琴
https://jsfiddle.net/ohd5r1ox/ https://jsfiddle.net/ohd5r1ox/
I have made some Corrections in your code,Please try this.. 我已经在您的代码中进行了一些更正,请尝试此操作。
function myFunctionHTML() { document.getElementById('Course_Description').innerHTML = "I like coding"; }; function myFunctionComp() { document.getElementById('Course_Description').innerHTML = "I like computers"; }; var H = document.getElementById('Course_HTML').value; var C = document.getElementById('Course_Comp').value; function myFunctionVar() { var Course_Select = document.getElementById('Course_Name').value; if (Course_Select == H) { myFunctionHTML(); } if (Course_Select == C) { myFunctionComp(); } };
<label class="w3-label w3-text-black">Course Name</label> <select id="Course_Name" name="Course_Name" onchange="myFunctionVar()" style="width:95%;" required> <option value="IntroHTML" id="Course_HTML">Intro to HTML and CSS</option> <option value="BuildComputers" id="Course_Comp">Build A Computer</option> </select> <label class="w3-label w3-text-black">Course Description</label> <p id="Course_Description">Hi there</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.