【发布时间】:2017-10-20 15:37:56
【问题描述】:
我正在创建一个客户偏好部分,客户可以通过单击两个单选按钮之一来选择他是否对水果感兴趣。问题是,我想通过允许空单选按钮/再次单击时取消选中单选按钮,让客户在水果上保留未定义的偏好。
JS 只是为通过 php 加载的带有checked="checked" 的单选按钮添加类,并向 CSS 的父标签添加一个类,因为在前端它显示为一个按钮组,如下所示: 我尝试查找堆栈溢出,但没有解决方案。
$('input:not(:checked)').parent().removeClass("radio_checked");
$('input:not(:checked)').removeAttr("checked");
$('input:checked').parent().addClass("radio_checked");
$('input').click(function() {
$('input:not(:checked)').parent().removeClass("radio_checked");
$('input:not(:checked)').removeAttr("checked");
$('input:checked').parent().addClass("radio_checked");
});
#customer_preferences_form{
margin-top: 50px;
padding-right: 6.5%;
}
.container_banana, .container_apple, .container_guava, .container_strawberry, .container_pineapple, .container_mango{
margin-left: 6.5%;
margin-top: 20px;
width: 43.5%;
float: left;
border: 1px solid grey !important;
border-radius: 6px;
padding: 0 !important;
overflow: hidden;
}
.field_title{
border-bottom: 1px solid grey !important;
margin: 0 !important;
padding: 5px 15px;
display: inline-block;
width: 100%;
float: left;
}
.field_title label{
text-align: center;
font-size: 18px!important;
font-weight: normal!important;
margin: 0 0 3px;
width: 100% !important;
display:inline-block;
cursor: default !important;
}
#customer_preferences_form label{
width: 50%;
display: inline-block;
float: left;
text-align: center;
padding: 5px 0px;
cursor: pointer;
}
#customer_preferences_form label:nth-child(2){
border-right: 1px solid black;
}
#customer_preferences_form input[type="radio"]{
display:none;
}
#customer_preferences_form .radio_checked{
color: #00d8a9;
background: #FAFAFA;
font-weight:bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container_mango">
<div class="field_title">
<label>Mango</label>
</div>
<label>
<input type="radio" name="mango" value="1"> Not Interested
</label>
<label>
<input type="radio" name="mango" value="2"> My Fav!
</label>
</div>
<div class="container_pineapple">
<div class="field_title">
<label>Pineapple</label>
</div>
<label>
<input type="radio" name="pineapple" value="1" checked="checked"> Not Interested
</label>
<label>
<input type="radio" name="pineapple" value="2"> My Fav!
</label>
</div>
【问题讨论】:
-
请更新我用缺少的 CSS 制作的 sn-p
-
你在找THIS
-
什么CSS? @mplungjan
-
没有@Nimish,它已经通过单击单选按钮工作,但您在问题中看到预览,单选按钮被隐藏,因为我们必须让它看起来像一个按钮组,它不能使用标签...
-
所以你想隐藏单选按钮并让标签像那样工作?请编辑 sn-p 并包含 css
标签: javascript jquery html css