简体   繁体   English

JavaScript onclick事件是否无效 <select><option>的?

[英]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. 除非我对我的JavaScript非常愚蠢,否则除了在<option>上触发的onclick事件之外,我看不出会出现什么问题。

 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: 将此函数添加到您的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: 只回答JS,而不是jQuery:

onclick event in option tag is just recognized by Firefox. 选项标签中的onclick事件刚刚被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. 如果您需要适用于所有浏览器(如IE或Chrome)的解决方案,则可以在“ 选择 ”元素上使用onchange事件。

HTML : 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: JS在html head部分中定义为脚本:

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/ JSFiddle样本工作正常: http//jsfiddle.net/amontellano/gLML3/14/

I hope it helps. 我希望它有所帮助。

DEMO 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/ 在这里: http//jsfiddle.net/gLML3/7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么“onfocus”不适用于多选选项,而“onclick”则可以? - why 'onfocus' does not work on multiple select option, and 'onclick' does? onclick事件不适用于multi_select过滤器-javascript - onclick event not work with multi_select filter - javascript Javascript:JSFiddle遇到麻烦-输出字符串的简单onclick事件不起作用 - Javascript: Trouble with JSFiddle - Simple onclick event to output String does not work Javascript 类实例方法不适用于反应中的 onClick 回调事件 - Javascript class instance method does not work with onClick callback event in react Javascript:事件处理程序在选择选项(IE)中不起作用 - Javascript : event handler doesn't work in an select option (IE) 删除/隐藏javascript中的select选项而不使其显得笨拙(需要延迟select元素的本机onclick事件) - Removing/hiding a select option in javascript without making it look awkward (need to delay the native onclick event of the select element) 为什么jQuery更改事件不能与select选项一起使用? - Why does jQuery change event not work with select option? 将onclick事件添加到html选择选项 - adding onclick event to html select option 从onclick事件中的select语句中选择一个选项 - selecting an option in a select statement from onclick event 如何防止onclick事件在选择选项中触发 - how to prevent onclick event to trigger in select option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM