【发布时间】:2014-01-28 13:48:54
【问题描述】:
由于某种原因,它下面的代码无法正常工作。除非我对我的 JavaScript 非常愚蠢,否则除了在<option>s 上没有触发的 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" />
【问题讨论】:
-
你需要钩住选择元素。
-
使用“onchange”事件。这些选项应被视为父选择的一部分,而不是单独的控件。
-
@laaposto OP 的示例不使用 jQuery...
-
@BrianDriscoll 如果需要,我可以使用 jQuery。我刚接触它,所以我不想查看所有这些,因为客户项目明天到期。
-
如果您有答案,请将其发布为答案,而不是评论!
标签: javascript html css forms