由于某种原因,它下面的代码无法正常工作。除非我对我的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" />
答案 0 :(得分:3)
将此功能添加到您的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" />
答案 1 :(得分:2)
只回答JS,而不是jQuery:
Firefox中只识别选项标签中的onclick 事件。如果您需要适用于所有浏览器(如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/
我希望它有所帮助。
答案 2 :(得分:0)
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;
}
}
答案 3 :(得分:-2)
只需将这些功能添加到您的代码中:
$('#drop_down').on('change', function(){
($(this).val() == 'other') ? showOther() : hideOther();
});