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

时间:2017-03-21 23:29:42

标签: javascript php jquery html css

嗨我有一个下拉菜单系统,第一部分有三个单选按钮伪装成图像。选择其中一个单选按钮后,我希望它将下一部分更改为相应的div(笔记本电脑,平板电脑或手机)。我感到困惑的部分是如何从单选按钮获取值而不必使用提交按钮。然后我想用php来回应正确的div。我想在运行if语句来比较单选按钮值之后我必须回显一个变量。我似乎无法找到如何在不使用提交按钮的情况下触发脚本。

JS FIDDLE(不包括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>';
    }


?>

我迷失了什么类型的功能以及我应该使用它们的方式,所以我不知道我填充了伪代码。

2 个答案:

答案 0 :(得分:0)

您可以获取单选按钮的值,并使用jquery .click()函数中的值执行某些操作,如:

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

答案 1 :(得分:0)

如果您在单选按钮上包含Value,如下所示,您实际上可以在每次点击/更改时提取值。

<强> HTML

<input type="radio" name="type" id="laptop_button" value="laptop" />
...

<input type="radio" name="type" id="tablet_button" value="tablet" />
...

<input type="radio" name="type" id="phone_button" value="phone" />
...

  <button class="accordion type-accordion">
    <h2 class="text-center">
      Model
      <span id="selectedType"></span>
    </h2>
  </button>

<强>码

$("input[name='type']").change(function(e) {
  var selectedType = $(this).val();
  $("#selectedType").html(selectedType.toUpperCase());
}

并突出显示代码问题。 下面的代码不起作用,因为您错过了#ID选择。要在jQuery中选择ID(按ID获取元素),您需要包含#tag。 例如$("#laptop_button")

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

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

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

为了好玩,我已经完成了你的屏幕类型和设备选择手风琴。

$(".accordion").click(function(e) {
  $(this).toggleClass("active");

  var nextPanel = $(this).next(".panel");
  console.log(nextPanel);

  if ($(this).hasClass("active")) {
    // showPanel(nextPanel);
    nextPanel.showPanel();

    // hide default_panel when type is already selected
    if ((nextPanel.attr("id") === "default_panel") && ($("#selectedType").html() !== "")) {
      // hidePanel(nextPanel);
      nextPanel.hidePanel();
    }

  } else {
    // hidePanel(nextPanel);
    nextPanel.hidePanel();
  }

});


$("input[name='type']").change(function(e) {

  var selectedType = $(this).val();
  var selectedTypeID = "#" + selectedType + "_panel";

  $("#selectedType").html(selectedType.toUpperCase());

  // hide all device panel
  // $(".device-panel")
  // 	.hide()
  //   .each(function() {
  //   	hidePanel($(this));
  //   });
  $(".device-panel").hidePanel();

  // hide default panel if device type is selected
  $("#default_panel").hidePanel();

  // ensure that type-accordion button is always 'active'
  $(".type-accordion").removeClass("active").addClass("active");

  console.log("selected device type", selectedType, $(selectedTypeID));

  // showPanel( $(selectedTypeID) );
  $(selectedTypeID).showPanel();
});


// extend jQuery object
(function($) {

  $.fn.showPanel = function() {
    $(this)
      .show()
      .css("height", $(this)[0].scrollHeight + "px")
      .css("max-height", $(this)[0].scrollHeight + "px");
  };

  $.fn.hidePanel = function() {
    $(this)
      .hide()
      .css("height", "0px")
      .css("max-height", "0px");
  };

}(jQuery));


/* 
function showPanel(panel) {
  panel
    .show()
    .css("height", panel[0].scrollHeight + "px")
    .css("max-height", panel[0].scrollHeight + "px");
}
    
function hidePanel(panel) {
  panel
  	.hide()
    .css("height", "0px")
    .css("max-height", "0px");
}
*/
#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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<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" value="laptop" /> <img height="70px" 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" value="tablet" /> <img height="70px" 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" value="phone" /> <img height="70px" src="https://cdns.iconmonstr.com/wp-content/assets/preview/2012/240/iconmonstr-smartphone-4.png" alt="phone"> </label>
  </div>

  <button class="accordion type-accordion">
        <h2 class="text-center">
            Model
            <span id="selectedType"></span>
            </h2>
      </button>


  <div class="panel" id="default_panel">
    <label>
          <h3 class="text-center">Please select a Device Type above</h3></label>
  </div>
  <div class="panel device-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>
  <div class="panel device-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>
  <div class="panel device-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>


</div>