根据下拉菜单显示多个字段
Show multiple fields based on dropdown
我试图显示/隐藏基于指定'other'的下拉菜单的字段,但只有第一个元素显示,我如何使所有字段与li id =" other"出现时选择?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Selectbox</title>
<style type="text/css">
#osother{display:none;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
if($(this).attr("value")=="otheros"){
$("#osother").hide();
$("#osother").show();
}
});
}).change();
});
</script>
</head>
<body>
<li>
<p>Operating System: <Select Class="selectmenu" id="whatever" name="OS" onchange="markDirty();" required>
<option value = "">-- Select an Option --</option>
<option value = "Win">Windows</option>
<option value = "ios">iOS</option>
<option value = "otheros">Other</option>
</select>
</p></li>
<li id="osother">
Please Specify: <label id="oslabel"><input name="Other OS" type="text" placeholder="Other OS" size="50" /></label></li>
<li id="osother">
Version: <label id="version"><input name="OSV" type="text" placeholder="OS Version" size="50" /></label></li>
</body>
</html>
您必须在文档中只使用一次ID,因此您必须将#osother
更改为.osother
。当下拉框的值从osother
更改时,也要隐藏字段。
JSFiddle在这里。