简体   繁体   English

在单独的位置中选择不同的选项时,如何更改元素的HTML <select>元件?

[英]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.

相关问题 更改 HTML Select 元素的选定选项:当我使用 cursor 时行为不同 - Change the selected option of HTML Select element: behavior is different when I use cursor 将类应用于单独的元素时,如何更改元素? - How can I change an element when a class is applied to a separate element? 使用Shift键时如何限制选择元素中的所选选项 - How can I limit selected options in select element when using shift key 如何向 html 中的 select 元素添加更多选项? - How can i add more options to a select element in html? 当“不相关”的html select元素未选择任何选项时,如何退出jQuery事件? - How can I exit from a jQuery event when an “unrelated” html select element has had no option selected? 使用Javascript选择链接时,如何隐藏HTML元素? - How can I hide a HTML element when a link is selected with Javascript? 如何删除大于 select 元素中所选选项的选项? - How can i remove the options which are greater than the selected one in a select element? 更改HTML Select元素的选定选项并提交,就像单击它一样 - Change selected option of an HTML Select element and submit as if I click on it 当已选择选项时,如何获取HTML Select元素的通知 - How to get a notification for an HTML Select element, when the option is already selected HTML选择选项:将selected属性设置为input元素无效 - HTML select options: setting the selected attribute to an input element has no effect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM