简体   繁体   English

使用单选按钮隐藏/显示div

[英]Hiding/showing div using radio buttons

I am trying to hide and show a single div when a radio button is selected in bootstrap. 我想在bootstrap中选择一个单选按钮时隐藏并显示一个div。 I cannot get it to work. 我无法让它发挥作用。 Here is my current code: http://jsfiddle.net/pLqmS/9/ 这是我目前的代码: http//jsfiddle.net/pLqmS/9/

    <div class="row demo-row">
        <div class="span7">
            <div id="opt1" class="option">
            <h5>Generation</h5>  
        </div>
        <div id="opt2" class="option" style="display: none;">
            <h5>Test</h5>
        </div>
        <div id="opt3" class="option" style="display: none;">
            <h5>Test</h5>
        </div>
        <div id="opt4" class="option" style="display: none;">
            <h5>Test</h5>
        </div>
    </div>

Here is my script: 这是我的脚本:

$('.phone-choice').click(function () {
$('.option').each(function () {
    if ($(this).is(':visible')) {
        $(this).stop().slideUp('slow');
    }
});
var id = $(this).val();
$('#opt' + id).stop().slideDown('slow');
});

Any ideas? 有任何想法吗?

You are duplicating the ids, you should never do that. 你正在复制id,你永远不应该这样做。 Not only that it makes the html invalid, but also the selector will end up selecting the element with the same id that appears first in DOM. 不仅它使html无效,而且选择器最终将选择具有在DOM中首先出现的相同id的元素。 Id of your radio button and target divs are same. 单选按钮和目标div的ID相同。 You can instead make them classnames. 你可以改为使它们成为类名。

With the simplified version you could do this: 使用简化版本,您可以执行此操作:

Markup 标记

  <div  class="option opt1">
                <h5>Generation</h5> 
        </div>
        <div  class="option opt2" style="display: none;">
                <h5>Test</h5>

        </div>
        <div class="option opt3" style="display: none;">
                <h5>Test</h5>

        </div>
        <div  class="option opt4" style="display: none;">
                <h5>Test</h5>

        </div>

Js JS

$('.phone-choice').change(function () {
    $('.option:visible').stop().slideUp('slow');
    var id = this.value;
    $('.' + id).stop().slideDown('slow');
});

Demo 演示

If you want to target based on position (index) of the element you could try this: 如果你想根据元素的位置(索引)进行定位,你可以试试这个:

$('.phone-choice').change(function () {
    var $this = this
    $('.option:visible').stop().slideUp('slow', function () {
       $('.option').eq($('phone-choice').index($this)).slideDown('slow');
    });
});

FIddle 小提琴

You have duplicated ids for your HTML elements. 您有HTML元素的重复ID。 Note that the input elements share ids of some of the div elements 请注意,输入元素共享某些div元素的id

 <div id="opt1" class="option" style="display: none;">
 <input type="radio" class="phone-choice" name="optionsRadios" id="opt1" value="opt1" data-toggle="radio" checked></label>

You also have an unclosed div tag 你还有一个未封闭的div标签

        <div class="span7">
            <div id="opt1" class="option">
            <h5>Generation</h5>  
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM