简体   繁体   中英

Javascript selected index onchange doesn't work at all

I am trying to create a select tag that holds values in it and if two out of the five values are selected another select box will show on the screen. I have tried searching for this and found where people are using javascript and using the onchange event but for some reason I cannot get it to work. The toggle id is the one I want to show up if individual or short term is selected if not I want it to be hidden. The code is below and thanks for your help in advance. I didn't include the javascript because I deleted it all but I have tried it a few different ways with no luck. I am thinking it is the way I have the form laid out.

HTML

<tr>                        
   <td class='txt'><span>&#42;</span>Coverage Needed?</td>  
<td>
   <select name="ins_type" id='ins_options' onChange="toggle();">
     <option>Select...</option>                             
     <option value="Group">Group Insurance</option>
     <option value="Individual">Individual Insurance</option>
     <option value="Short Term">Short Term Insurance</option>                                   
     <option value="Medicare">Medicare</option>
     <option value="Life">Life Insurance</option>   
   </select>                
</td>                                   
<td ><div id='toggle'><span>&#42;</span>How Many?
   <select name='applicants' id='applicants' >                                      
      <option>Select...</option>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
      <option value='4'>4</option>
      <option value='5'>5</option>
      <option value='6'>6</option>
      <option value='7'>7</option>
      <option value='1'>8</option>
      <option value='9'>9</option>
      <option value='10'>10</option>    
   </select>
  </div>
  </td>             
</tr>

Javascript (I was thinking something like this should work)

<script type="text/javascript">
  function toggle() {
    if (document.getElementById("ins_options").options
    [getElementById("ins_options").selectedIndex].value == "Individual") {
       document.getElementById("toggle").style.display = "none";
    } else {
       document.getElementById("toggle").style.display = "block";
    }
  }
</script> 

You're missing document in your selected index lookup - http://jsfiddle.net/VfEyW/

So instead of

[getElementById("ins_options").selectedIndex]

try

[document.getElementById("ins_options").selectedIndex]

...

UPDATE

To check against 2 values you can do this

  function toggle() {
    var val = document.getElementById("ins_options").options[document.getElementById("ins_options").selectedIndex].value;

    if ( val == "Individual" || val == "Short Term" ) {
       document.getElementById("toggle").style.display = "none";
    } else {
       document.getElementById("toggle").style.display = "block";
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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