[英]why 'onfocus' does not work on multiple select option, and 'onclick' does?
[英]Does the JavaScript onclick event not work on <select> <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" />
将此函数添加到您的JS:
function showHideOther(){
if (document.getElementById('drop_down').value == 'other') {
showOther();
} else {
hideOther();
}
}
并改变你的选择元素,如下所示:
<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" />
只回答JS,而不是jQuery:
选项标签中的onclick事件刚刚被Firefox识别。 如果您需要适用于所有浏览器(如IE或Chrome)的解决方案,则可以在“ 选择 ”元素上使用onchange事件。
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在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样本工作正常: http : //jsfiddle.net/amontellano/gLML3/14/
我希望它有所帮助。
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;
}
}
只需将这些功能添加到您的代码中:
$('#drop_down').on('change', function(){
($(this).val() == 'other') ? showOther() : hideOther();
});
在这里: http : //jsfiddle.net/gLML3/7/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.