Does the JavaScript onclick event not work on <select>

后端 未结 4 1790
青春惊慌失措
青春惊慌失措 2020-12-20 06:11

For some reason the code below it is not working correctly. Unless I\'m being quite stupid with my JavaScript I can\'t see what\'s going wrong besides the onclick events not

相关标签:
4条回答
  • 2020-12-20 06:46

    DEMO

    var dropdown = document.getElementById('drop_down');
    
    dropdown.onchange = function() {
      var selected = dropdown.options[dropdown.selectedIndex].value;
    
      switch(selected) {
        case 'other':
          document.getElementById('other').value = "";
          document.getElementById('other').style.display = 'block'; 
          document.getElementById('otherBR').style.display = 'block';
          break;
        default:
          document.getElementById('other').style.display = 'none'; 
          document.getElementById('otherBR').style.display = 'none';
          break;
      }
    }
    
    0 讨论(0)
  • 2020-12-20 07:00

    Add this function to your JS:

    function showHideOther(){
        if (document.getElementById('drop_down').value == 'other') {
             showOther();   
        } else {
             hideOther();   
        }
    }
    

    And change your select element like this:

    <select name="" id="drop_down" onchange="showHideOther();">
            <option value="choose">Please choose</option>
            <option value="Allure">Allure</option>
            <option value="Elle">Elle</option>
            <option value="In-Style">In-Style</option>
            <option value="other">Other</option>
    </select>
    

    function showHideOther() {
      if (document.getElementById('drop_down').value == 'other') {
        showOther();
      } else {
        hideOther();
      }
    }
    
    function showOther() {
      document.getElementById('other').value = "";
      document.getElementById('other').style.display = 'block';
      document.getElementById('otherBR').style.display = 'block';
    }
    
    function hideOther() {
      document.getElementById('other').style.display = 'none';
      document.getElementById('otherBR').style.display = 'none';
    }
    #other {
      display: none;
    }
    #otherBr {
      display: none;
    }
    <select name="" id="drop_down" onchange="showHideOther();">
      <option value="choose">Please choose</option>
      <option value="Allure">Allure</option>
      <option value="Elle">Elle</option>
      <option value="In-Style">In-Style</option>
      <option value="other">Other</option>
    </select>
    <input type="text" name="fields_where" id="other" placeholder="Other" />
    <br id="otherBR" />

    0 讨论(0)
  • 2020-12-20 07:05

    just add these function to your code:

    $('#drop_down').on('change', function(){
        ($(this).val() == 'other') ? showOther() : hideOther();
    });
    

    se here: http://jsfiddle.net/gLML3/7/

    0 讨论(0)
  • 2020-12-20 07:13

    An answer with JS only, not jQuery:

    onclick event in option tag is just recognized by Firefox. If you need a solution that works on all browsers such as IE or Chrome you can use onchange event on your "Select" element.

    HTML :

    <select name="" id="drop_down" onchange="showHideOther(this.value);">
        <option value="choose" ">Please choose</option>
        <option value="Allure">Allure</option>
        <option value="Elle" >Elle</option>
        <option value="In-Style" >In-Style</option>
        <option value="other" id="otherOption">Other</option>
    </select>
    

    JS defined in the html head section as script:

    function showHideOther(value){
        alert(value);
        if (value==='other'){
            document.getElementById('other').value = "";
            document.getElementById('other').style.display = 'block'; 
            document.getElementById('otherBR').style.display = 'block';
        }
        else{
                document.getElementById('other').style.display = 'none'; 
                document.getElementById('otherBR').style.display = 'none';
        }
    }
    

    JSFiddle sample working fine: http://jsfiddle.net/amontellano/gLML3/14/

    I hope it helps.

    0 讨论(0)
提交回复
热议问题