最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Switch and Select in JavaScript - Stack Overflow

programmeradmin4浏览0评论

I'm new in JavaScript.. Here is my question.. I have drop down list :

<div class="spanNav-row" id="spanNav-row">
  <select name="timeDropList">

    <option value="6">6</option>
    <option value="10">10</option>
    <option value="15">15</option>
    <option value="30">30</option>
    <option value="60" selected>60</option>
    <option value="Vac">Vacation</option>

  </select>
</div>

and I have a switch :

switch () {

  case 6:
    break;
  case 10:
    break;
  case 15:
    break;
  case 30:
    break;
  case 60:
    break;
  default:
    break;
}

How do you transfer values of drop down list to the switch?

I'm new in JavaScript.. Here is my question.. I have drop down list :

<div class="spanNav-row" id="spanNav-row">
  <select name="timeDropList">

    <option value="6">6</option>
    <option value="10">10</option>
    <option value="15">15</option>
    <option value="30">30</option>
    <option value="60" selected>60</option>
    <option value="Vac">Vacation</option>

  </select>
</div>

and I have a switch :

switch () {

  case 6:
    break;
  case 10:
    break;
  case 15:
    break;
  case 30:
    break;
  case 60:
    break;
  default:
    break;
}

How do you transfer values of drop down list to the switch?

Share Improve this question edited Jul 10, 2011 at 18:39 Lance Roberts 22.9k32 gold badges114 silver badges132 bronze badges asked Jul 10, 2011 at 18:25 SergioSergio 5293 gold badges12 silver badges26 bronze badges 1
  • Demos: jsfiddle/userdude/PCGjU You'll need to have either Chrome Console or Firefox Firebug console open to see output. – Jared Farrish Commented Jul 10, 2011 at 18:40
Add a ment  | 

3 Answers 3

Reset to default 6

Standard JS:

var ddl = document.getElementById("timeDropList");
var selectedValue = ddl.options[ddl.selectedIndex].value;

switch (selectedValue) {}

jQuery (in case you're interested):

switch($('#timeDropList option:selected').val()) {}

NOTE: To reference the drop down by getElementById(), timeDropList will also have to be the ID, not just the name:

<select id="timeDropList" name="timeDropList">
var swi_var = document.getElementById('spanNav-row').value;

swi_var can go in the switch

If you use jQuery, it would be piece a cake:

var selectedValue = $('#spanNav-row').val();
switch (selectedValue) {
    // Your switch body here.
}
发布评论

评论列表(0)

  1. 暂无评论