如何使用单选按钮使用javascript显示div

时间:2017-03-21 19:28:05

标签: javascript jquery html css

我试图让这个手风琴成为某些项目的选择器。因此,如果按下按钮(这实际上是一个单选按钮),快速分解,那么它下方的面板使三个选项中的一个可见。它们每个都有一个名为invisible的类,它使它们隐藏起来,我想制作哪个类似于所选单选按钮的id可见。

图片示例 enter image description here

出于某种原因,它们并非隐形,只有一个显示它与我的手风琴JavaScript有关吗?

HTML

<div id="pricing_holder">

                <button class="accordion">
                    <h2 class="text-center">Screen Type</h2></button>
                <div class="panel text-center" id="type_panel">
                    <label class="icon-select">
                        <input type="radio" name="type" id="laptop_button" /> <img src="https://iconmonstr.com/wp-content/g/gd/makefg.php?i=../assets/preview/2012/png/iconmonstr-laptop-4.png&r=0&g=0&b=0" alt="laptop"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="tablet_button" /> <img src="https://iconmonstr.com/wp-content/g/gd/makefg.php?i=../assets/preview/2012/png/iconmonstr-tablet-1.png&r=0&g=0&b=0" alt="tablet"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="phone_button" /> <img src="https://cdns.iconmonstr.com/wp-content/assets/preview/2012/240/iconmonstr-smartphone-4.png" alt="phone"> </label>
                </div>
                <button class="accordion">
                    <h2 class="text-center">Model</h2></button>
                <div class="panel invisible" id="laptop_panel">
                    <div id="col1">
                        <label class="control control--radio">LAPTOP
                            <input type="radio" name="radio-laptop" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col3">
                        <label class="control control--radio">LAPTOP2
                            <input type="radio" name="radio-laptop" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col2">
                        <label class="control control--radio">LAPTOP3
                            <input type="radio" name="radio-laptop" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                </div>
                <div class="panel invisible" id="tablet_panel">
                    <div id="col1">
                        <label class="control control--radio">TABLET1
                            <input type="radio" name="radio-tablet" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col3">
                        <label class="control control--radio">TABLET2
                            <input type="radio" name="radio-tablet" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col2">
                        <label class="control control--radio">TABLET3
                            <input type="radio" name="radio-tablet" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                </div>
                <div class="panel invisible" id="phone_panel">
                    <div id="col1">
                        <label class="control control--radio">iPhone 3
                            <input type="radio" name="radio-phone" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col3">
                        <label class="control control--radio">Microsoft Lumia 430
                            <input type="radio" name="radio-phone" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                    <div id="col2">
                        <label class="control control--radio">Galaxy S3
                            <input type="radio" name="radio-phone" />
                            <div class="control__indicator"></div>
                        </label>
                    </div>
                </div>


            </div>

CSS

#pricing_holder {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    padding-bottom: 20px;
}

.icon-select {
    margin-right: 20px;
    margin-left: 20px;
}

#col1 {
    float: left;
    width: 285px;
    height: 65px;
}

#col2 {
    float: none;
    height: 65px;
    overflow: hidden;
    display: table-cell;
}

#col3 {
    float: right;
    height: 65px;
}

button.accordion {
    background-color: #ffffff;
    color: #444;
    cursor: pointer;
    padding: 12px;
    width: 75%;
    text-align: left;
    outline: none;
    transition: 0.4s;
    border-left: 1px solid grey;
    border-right: 1px solid grey;
    border-top: 1px solid grey;
    border-radius: 4px;
    border-bottom: none;
}

button.accordion.active,
button.accordion:hover {
    background-color: #6fdd7a;
    color: #ffffff;
}

div.panel {
    padding: 0px 18px;
    background-color: #ffffff;
    max-height: 0;
    overflow: hidden;
    width: 71%;
    display: block;
    transition: max-height 0.2s ease-out;
    border: 1px solid grey;
}

label > input {
    visibility: hidden;
    position: absolute;
}

label > input + img {
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 2px;
    -webkit-transition: all 0.25s linear;
}

label > input:checked + img {
    background-color: #6fdd7a;
}

.invisible {
    display: none;
}

.control {
    font-size: 18px;
    position: relative;
    display: block;
    margin-bottom: 15px;
    padding-left: 30px;
    cursor: pointer;
}

.control input {
    position: absolute;
    z-index: -1;
    opacity: 0;
}

.control__indicator {
    position: absolute;
    top: 2px;
    left: 0;
    width: 20px;
    height: 20px;
    background: #e6e6e6;
}

.control--radio .control__indicator {
    border-radius: 50%;
}

.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
    background: #ccc;
}

.control input:checked ~ .control__indicator {
    background: #6fdd7a;
}

.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
    background: #6fdd7a;
}

.control__indicator:after {
    position: absolute;
    display: none;
    content: '';
}

.control input:checked ~ .control__indicator:after {
    display: block;
}

.control--checkbox .control__indicator:after {
    top: 4px;
    left: 8px;
    width: 3px;
    height: 8px;
    transform: rotate(45deg);
    border: solid #fff;
    border-width: 0 2px 2px 0;
}

.control--checkbox input:disabled ~ .control__indicator:after {
    border-color: #7b7b7b;
}

.control--radio .control__indicator:after {
    top: 7px;
    left: 7px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #fff;
}

.control--radio input:disabled ~ .control__indicator:after {
    background: #7b7b7b;
}

JS

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  }
}

var typeselect = $("input[name='type']:checked").attr('id');

$("laptop_button").click(function(){
    $("laptop_panel").toggleClass("invisible");
});

$("tablet_button").click(function(){
    $("tablet_panel").toggleClass("invisible");
});

$("phone_button").click(function(){
    $("phone_panel").toggleClass("invisible");
});

JSFIDDLE:https://jsfiddle.net/4crj2ash/

0 个答案:

没有答案