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

时间:2016-12-23 05:10:34

标签: javascript html

因此,在我的网站上,我尝试制作它,以便当您使用&lt; select&gt;选择某个课程名称时您还可以在其下方更改“课程说明”的HTML,以便“说明”与课程相对应。这是我到目前为止所得到的: &#13; &#13; &LT;脚本&gt;&#13;   function myFunctionHTML(){&#13;     document.getElementById(&#39; Course_Description&#39;)。innerHTML =&#34;我喜欢编码&#34;&#13;   };&#13; &#13; function myFunctionComp(){&#13;   document.getElementById(&#39; Course_Description&#39;)。innerHTML =&#34;我喜欢计算机&#34;&#13; };&#13; var H = document.getElementById(&#39; Course_HTML&#39;)。innerHTML;&#13; var C = document.getElementById(&#39; Course_Comp&#39;)。innerHTML;&#13; var Course_Select = document.getElementById(&#39; Course_Name&#39;)。innerHTML;&#13; &#13; function myFunctionVar(){&#13;   如果Course_Select = H {&#13;     myFunctionHTML();&#13;   }&#13;   如果Course_Select = C {&#13;     myFunctionComp();&#13;   }&#13; }; &LT; /脚本&GT;&#13; &lt; label class =&#34; w3-label w3-text-black&#34;&gt;课程名称&lt; / label&gt;&#13; &lt; select id =&#34; Course_Name&#34;命名=&#34; COURSE_NAME&#34;平变化=&#34; myFunctionVar()&#34;风格=&#34;宽度:95%;&#34;所需&GT;&#13;   &lt; option value =&#34; IntroHTML&#34; id =&#34; Course_HTML&#34;&gt; HTML和CSS简介&lt; / option&gt;&#13;   &lt; option value =&#34; BuildComputers&#34; id =&#34; Course_Comp&#34;&gt;构建计算机&lt; / option&gt;&#13; &LT; /选择&GT;&#13; &lt; label class =&#34; w3-label w3-text-black&#34;&gt;课程描述&lt; / label&gt;&#13; &lt; p id =&#34; Course_Description&#34;&gt;嗨那里&lt; / p&gt;&#13; &#13; &#13; 我非常确定myFunctionVar()是错的,但在研究了其他网站和其他问题后,我似乎找不到任何东西。

6 个答案:

答案 0 :(得分:1)

不需要编写那么多代码来通过innerHTML选项中的更改来更改元素的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>

答案 1 :(得分:0)

首先,删除&#34;;&#34;从功能。然后,你应该得到option的value属性而不是innerHTML。

答案 2 :(得分:0)

首先获取所选选项的值,然后更改说明

  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>

答案 3 :(得分:0)

这里是您的无错误代码尝试替换它,您的代码和测试是按预期工作的。 已从您的代码中删除; 修正了`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();
  }
}

答案 4 :(得分:0)

这是完整的代码

    <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>

这是小提琴

https://jsfiddle.net/ohd5r1ox/

答案 5 :(得分:0)

我在你的代码中做了一些更正,请试试这个..

&#13;
&#13;
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();
  }
};
&#13;
<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>
&#13;
&#13;
&#13;