doumo0206 2017-03-21 23:29
浏览 53

在php中使用单选按钮值可以显示三个div中的一个

Hi I have a dropdown menu system that has three radio buttons in the first section that are disguised as images. After selecting one of these radio buttons I would like it for it to change the next section to the corresponding div (laptop, tablet or phone). The part that I am confused on is how to get the value from the radio button WITHOUT having to use a submit button. I would then like to use php to echo out the correct div. I'm thinking that I would have to echo out a variable after running an if statement to compare the radio button values. I just can't seem to find out how to trigger the script without using a submit button.

JS FIDDLE (Doesn't include php)

https://jsfiddle.net/4crj2ash/

HTML

   <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" value="laptop" /> <img src="icons/iconmonstr-laptop-4-120.png" alt="laptop"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="tablet_button" value="tablet" /> <img src="icons/iconmonstr-tablet-1-120.png" alt="tablet"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="phone_button" value="phone" /> <img src="icons/iconmonstr-smartphone-3-120.png" alt="phone"> </label>
                </div>
                <button class="accordion">
                    <h2 class="text-center">Model</h2></button>

               <?php echo $finalval; ?>

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

PHP

 <?php

    if (radval == 'phone') {
        $finalval = '<div class="panel" 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>';
    } elseif (radval == 'tablet') {
        $finalval = ' <div class="panel" 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>';
    } elseif (radval == 'laptop') {
        $finalval = '<div class="panel" 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>';
    } else {
        echo '<p>Something went wrong</p>';
    }


?>

I'm lost on what types of functions and way I should use them so for what i didn't know i filled with pseudo code.

  • 写回答

2条回答 默认 最新

  • duanqi5114 2017-03-21 23:41
    关注

    You can get the value of the radio button and do something with the value within your jquery .click() function like:

    $("laptop_button").click(function(){
        var radioValue = $(this).val();
        function_that_does_something_with_the_value(radioValue); // needs to be a javascript funtion
        $("laptop_panel").toggleClass("invisible");
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line