根据下拉菜单显示多个字段

Show multiple fields based on dropdown

本文关键字:字段 显示 下拉菜单      更新时间:2023-09-26

我试图显示/隐藏基于指定'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在这里。

<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;
}

这里有几个错误:

  1. 您需要将li封装在ul

  2. id需要是唯一的-因为它被认为是,选择器将只返回(第一个)元素,你应该使用例如一个类代替,允许你选择多个元素在同一组

  3. 在你的HTML中你大写了ClassSelect,这些应该是小写的

演示小提琴

HTML

<ul>
    <li>
        <p>Operating System:
            <select class="selectmenu" id="whatever" name="OS" onchange="markDirty();" required='required'></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();
            }
        });
    }).change();
});