单选按钮和隐藏的字段

时间:2013-08-24 14:02:17

标签: javascript jquery

我正在尝试创建一个单击单选按钮时出现的列表,单击按钮后列表显示的方式出现问题。

以下是示例http://jsfiddle.net/c2webdev/4bpKE/

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<style>
    .none {
        display: none;
    }
</style>
</head>

<body>
<input type="radio" name='thing' value='valuable' data-id="bank"/>Running Event
<input type="radio" name='thing' value='valuable' data-id="school"/>Other challenges
<input type="radio" name='thing' value='valuable' data-id="bakery"/>Baking

<div id="school" class="none">
    <label for="name">What was the activity called?</label>
    <select id="myList">
        <option value="1">List item 1</option>
        <option value="2">List item 2</option>
        <option value="3">List item 3</option>
        <option value="4">List item 4</option>
    </select>
</div>
<div id="bank" class="none">
    <label for="name">What was the activity called?</label>
    <select id="myList">
        <option value="1">Pancake Making</option>
        <option value="2">Egg and spoon race</option>
        <option value="3">Sack race</option>
        <option value="4">Three leg race</option>
    </select></div>
<div id="bakery" class="none">
    <label for="name">What was the activity called?</label>
    <select id="myList">
        <option value="1">Pancake Making</option>
        <option value="2">Egg and spoon race</option>
        <option value="3">Sack race</option>
        <option value="4">Three leg race</option>
    </select></div>
<script>
    $(':radio').change(function (event) {
        var id = $(this).data('id');
        $('#' + id).addClass('none').siblings().removeClass('none');
    });
</script>
</body>
</html>

请帮忙

2 个答案:

答案 0 :(得分:0)

您没有设置库,例如左窗格中的jQuery

答案 1 :(得分:0)

我在您的选择容器中添加了一个类名.select。如果你以后添加了另一台收音机,这种方式就可以了。所以:

<强> CSS:

.select {
    display: none;
}

<强> JS:

$('input[type="radio"]').click(function (event) {
    $(".select").hide();
    var id = $(this).data('id');
    $('#' + id).show();
});

<强> HTML:

<input type="radio" name='thing' value='valuable' data-id="bank" />Running Event
<input type="radio" name='thing' value='valuable' data-id="school" />Other challenges
<div id="school" class="select">
    <label for="myList1">What was the activity called?</label>
    <select id="myList1">
        <option value="1">Virgin London Marathon</option>
        <option value="2">Round the Island cycle challenge</option>
        <option value="3">Kilimanjaro trek</option>
        <option value="4">Thames Path Challenge</option>
    </select>
</div>
<div id="bank" class="select">
    <label for="myList2">What was the activity called?</label>
    <select id="myList2">
        <option value="1">Pancake Making</option>
        <option value="2">Egg and spoon race</option>
        <option value="3">Sack race </option>
        <option value="4">Three leg race</option>
    </select>
</div>

See Demo by jquery enabled