Does the JavaScript onclick event not work on <select>

后端 未结 4 1854
青春惊慌失措
青春惊慌失措 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 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 :

    
    

    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.

提交回复
热议问题