简体   繁体   中英

Does the JavaScript onclick event not work on <select> <option>'s?

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 firing on the <option> s.

 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"> <option value="choose" onclick="hideOther();">Please choose</option> <option value="Allure" onclick="hideOther();">Allure</option> <option value="Elle" onclick="hideOther();">Elle</option> <option value="In-Style" onclick="hideOther();">In-Style</option> <option value="other" id="otherOption" onclick="showOther();">Other</option> </select> <input type="text" name="fields_where" id="other" placeholder="Other" /> <br id="otherBR" /> 

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

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.

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

just add these function to your code:

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

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

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