简体   繁体   English

动态地向选择框添加选项

[英]Adding options to a select box dynamically

I am trying to add options to select element using javascript dynamically for a university assignment. 我正在尝试添加选项以使用javascript动态选择元素用于大学作业。

We are creating a class booking system for a gym so users can book into a class. 我们正在为健身房创建一个班级预订系统,以便用户可以预订课程。 I wanted to use drop down boxes, so you select the class, then the time using the next drop down box, so it has to change depending on the class selected. 我想使用下拉框,因此您选择类,然后使用下一个下拉框选择时间,因此必须根据所选的类进行更改。 Finally, the user would select the personal trainer in the last box, once again created depending on what timeslot is selected. 最后,用户将在最后一个框中选择私人教练,再次根据选择的时间段创建。

This is what I have so far (javascript side): 这就是我到目前为止(javascript方面):

<script type="text/javascript">
        function getTimes()
        {
            var index=document.getElementById("classes").selectedIndex;
            var x=document.getElementById("schedule");

            if(index==0)
            {
                document.getElementById("schedule").value=" ";
            }
            else if(index==1)
            {
                var option=document.createElement("option");
                option.text="Monday 8:00pm";
                try
                {
                     // for IE earlier than version 8- from w3 schools
                    x.add(option,x.options[null]);
                }
                catch (e)
                {
                    x.add(option,null);
                 }

            } 
                        }

and html: 和HTML:

<div>
 <span class="label">Class:</span>      
 <select class="dropdown" id="classes" name="classes"     onChange="getTimes();">
        <option value="none"> </option>
            <option value="pilates">Pilates</option>
        <option value="crossfit">Cross Fit</option>
  </select>
  </div>

<div>
  <span class="label">Time:</span>
  <select class="dropdown" id="schedule"></select>
</div>

 <div>
  <span class="label">Trainer:</span>
  <select class="dropdown" id="trainer"></select>
  </div>

To the code seems like it should work, but for whatever reason when I select the first class, in this case "Pilates" the "Time" drop down box is still blank. 对于代码似乎应该可以工作,但无论出于何种原因,当我选择第一个类时,在这种情况下“普拉提”,“时间”下拉框仍然是空白的。

Can anyone tell me where I am going wrong? 谁能告诉我哪里出错了?

The error is in the first line of the function 错误位于函数的第一行

function getTimes();

                   ^------ You have a semi colon here
                           which is not supposed to be there

Also is a better idea to cache your selectors if you are referencing the same element again. 如果再次引用相同的元素,也可以更好地缓存选择器。 Bind the events using Javascript instead of binding them inline. 使用Javascript绑定事件而不是内联绑定它们。

// Store you selectors so that you can use later
// This will improve your performance
var classDropdown = document.getElementById("classes"),
    scheduleDropdown = document.getElementById("schedule");
// Abbd events using javascript
classDropdown.addEventListener('change', getTimes);

function getTimes() {
      var index = classDropdown.selectedIndex;

      if (index == 0) {
          scheduleDropdown.value = " ";
      } else if(index == 1) {
          var option = document.createElement("option");
          option.text = "Monday 8:00pm";
          try {
              // for IE earlier than version 8- from w3 schools
              scheduleDropdown.add(option, x.options[null]);
          } catch (e) {
              scheduleDropdown.add(option, null);
          }
      }
  }

Check Demo 检查演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM