如何制作交互式选项按钮
How to make interactive option buttons
这就是我想要实现的:按钮。
我不知道如何使按钮包含表单数据,只有选定的按钮post到PHP当我点击提交。
到目前为止我写的是:
var count = 0;
function setColor(btn, color) {
var property = document.getElementById(btn);
if (count == 0) {
property.style.backgroundColor = "#4d4d4d"
property.style.color = "#ffffff"
count = 1;
} else {
property.style.backgroundColor = "#ffffff"
property.style.color = '#0000ff'
count = 0;
}
}
<button type="button" id="button1" onClick="setColor('button1', '#101010')">
<input type="radio" name="rooms" value="1">1
<br>
</button>
<button type="button" id="button2" onClick="setColor('button2', '#101010')">
<input type="radio" name="rooms" value="2">2
<br>
</button>
也帮助重置其他按钮的颜色,当我点击一个新的选项将不胜感激。
问题是您的按钮有一个onclick
侦听器,但侦听器也由子元素上的事件触发。所以你可以点击按钮而不是复选框,它会改变它的颜色而不改变值。
你应该使用标签代替,使用CSS3你可以不使用JavaScript。
HTML:<div>
<input type="checkbox" id="option1" name="rooms" />
<label for="option1">1</label>
</div>
<div>
<input type="checkbox" id="option2" name="rooms" />
<label for="option2">2</label>
</div>
CSS: input[type="checkbox"]{
display: none;
}
label{
width: 40px;
height: 20px;
color: #4d4d4d;
background: #FFF;
}
input[type="checkbox"]:checked + label{
background: #4d4d4d;
color: #FFF;
}
如果您同意只使用一点点JavaScript,那么下面将是您所需要的:
代码:
var buttons = document.querySelectorAll("button[id^=button]");
[].forEach.call(buttons, function(button) {
button.onclick = function() {
/* Remove the 'checked' attribute from the other inputs */
[].forEach.call(buttons, function(each) {
if (button !== each) [].forEach.call(each.children, function(child) {
if (child.nodeName === "INPUT") child.removeAttribute("checked");
});
});
/* Check the child input */
document.querySelector(this.getAttribute("data-check")).setAttribute("checked", "");
/* Remove the 'clicked' attribute from every button */
[].forEach.call(buttons, function(each) {
each.removeAttribute("clicked");
});
/* Add the 'clicked' attribute to the clicked button */
this.setAttribute("clicked", "");
}
});
指出:
为了得到你想要的外观,
input
本身必须隐藏,所以我们必须使用JavaScript来自动检查input
当按钮被点击。正如您所看到的,我已经为每个按钮添加了
data-check
属性。我这样做是为了避免必须遍历每个按钮的每个子按钮才能找到输入并取消选中它们。它基本上包含了它的input
子元素的选择器,这样我们就可以使用它来查找document.querySelector()
的输入。
下面是一个代码片段演示解决方案:
/* ----- JavaScript ----- */
var buttons = document.querySelectorAll("button[id^=button]");
[].forEach.call(buttons, function(button) {
button.onclick = function() {
/* Remove the 'checked' attribute from the other inputs */
[].forEach.call(buttons, function(each) {
if (button !== each)[].forEach.call(each.children, function(child) {
if (child.nodeName === "INPUT") child.removeAttribute("checked");
});
});
/* Check the child input */
document.querySelector(this.getAttribute("data-check")).setAttribute("checked","");
/* Remove the 'clicked' attribute from every button */
[].forEach.call(buttons, function(each) {
each.removeAttribute("clicked");
});
/* Add the 'clicked' attribute to the clicked button */
this.setAttribute("clicked", "");
}
});
/* ----- CSS ----- */
input[name="rooms"] {
display: none;
}
button {
background-color: white;
border: 1px solid grey;
border-radius: 50%;
color: dodgerblue;
cursor: pointer;
font-size: 1em;
font-weight: 600;
height: 50px;
outline: none;
transition: background-color .3s ease, color .3s ease;
width: 50px;
}
button[clicked] {
color: white;
background-color: grey;
}
<!----- HTML ----->
<div>
<p style = "font-size: 1.3em">Rooms</p>
<div id="button-wrapper">
<button type="button" id="button1" data-check="[name='rooms'][value='1']" clicked>
<input type="radio" name="rooms" value="1" checked/><span>1</span>
</button>
<button type="button" id="button2" data-check="[name='rooms'][value='2']">
<input type="radio" name="rooms" value="2" /><span>2</span>
</button>
<button type="button" id="button2" data-check="[name='rooms'][value='3']">
<input type="radio" name="rooms" value="3" /><span>3</span>
</button>
<button type="button" id="button2" data-check="[name='rooms'][value='4+']">
<input type="radio" name="rooms" value="4+" /><span>4+</span>
</button>
</div>
</div>
相关文章:
- 如何在不需要单击按钮的情况下获取选项的值
- 单击按钮即可在浏览器的新选项卡中打开 Sapui5 详细信息页面
- 复选框/单选按钮-添加所选项目的总价
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 更新:仅根据单选按钮和所选选项选择特定项目
- 如何在ASP.NET中单击按钮后激活启动选项卡
- Angular:如何使用ngRepeat并设置默认单选按钮选项
- 使用按钮引用单选按钮选项
- 记住jQuery测验中的单选按钮选项
- 数据禁用按钮选项在 Chrome 上不起作用
- 选择任何一个单选按钮后,单选按钮选项卡焦点无法正常工作
- 使用量角器和黄瓜选择单选按钮选项时出错
- 主干表单提交按钮选项不起作用
- 将用户选择的多个单选按钮选项存储在使用 ng-repeat(s)构建的角度 js 中
- 如何验证单选按钮选项
- Shopify为每个产品创建单选按钮选项
- 不能在Angular.js中使用ng-repeat设置默认选中的单选按钮选项
- 如何基于单选按钮选项触发不同的功能
- 如何将javascript消息弹出框替换为只有ok按钮选项的Jquery消息弹出框
- 我如何在我的Knockout.js向导中获得单选按钮选项的值