根据下拉菜单显示多个字段 [英] Show multiple fields based on dropdown

查看:72
本文介绍了根据下拉菜单显示多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试根据指定其他的下拉菜单显示/隐藏字段,但只有第一个元素显示,如果选择,我如何显示所有字段li id =osother?

I'm trying to show / hide fields based on the drop down menu specifying 'other', however only the first element shows, how do I make all fields with li id = "osother" appear when selected?

<!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 更改下拉菜单的值时,应隐藏字段。

You must use ID only once in a document, so you have to change #osother to .osother. And also fields should be hidden when the value of dropdown is changed from osother.

JSFiddle 在这里。

HTML / p>

HTML

<ul>
<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 class="osother">Please Specify:
    <label id="oslabel">
        <input name="Other OS" type="text" placeholder="Other OS" size="50" />
    </label>
</li>
<li class="osother">Version:
    <label id="version">
        <input name="OSV" type="text" placeholder="OS Version" size="50" />
    </label>
</li>
</ul>

JS

$(document).ready(function () {
$("select").change(function () {
    $("select option:selected").each(function () {
        if ($(this).attr("value") == "otheros") {
            $(".osother").show();
        } else {
            $(".osother").hide();
        }
    });
}).change();
});

CSS

.osother {
    display:none;
}

这篇关于根据下拉菜单显示多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆