嗨,我有一个下拉框系统,第一部分有三个伪装成图像的单选按钮。选择其中一个单选按钮后,我希望它能将下一节更改为相应的div(笔记本电脑、平板电脑或手机)。我困惑的部分是如何从单选按钮中获取值,而不必使用提交按钮。然后我想使用php来回显正确的div。我在想,在运行if语句比较单选按钮值后,我必须回显一个变量。我似乎找不到如何在不使用提交按钮的情况下触发脚本。
JS文件(不包括php)
https://jsfiddle.net/4crj2ash/
超文本标记语言
<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>';
}
?>
我不知道我应该使用什么类型的函数和方式,所以我不知道我用伪代码填充。
您可以获取单选按钮的值,并在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");
});
如果您在单选按钮上包含值,如下所示,您实际上可以在每次单击/更改时提取值。
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>