gpt4 book ai didi

jquery - CSS更改 radio 的主动选择

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:46 24 4
gpt4 key购买 nike

我正在使用以下内容来创建切换开关而不是单选按钮 - 它效果很好

.switch
{
display: block;
float: left;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
font-weight: bold;
text-transform: uppercase;
background: #eee;
border: 1px solid #aaa;
padding: 2px;
box-shadow: inset 0 2px 5px rgba(0,0,0,0.2);
margin-left: 20px;
}

.switch input[type=radio]
{
display: none;
}

.switch label
{
display: block;
float: left;
padding: 3px 6px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
color: #aaa;
cursor: pointer;
}

.switch label:hover
{
text-shadow: 0 0 2px #fff;
color: #888;
}

.switch label.checked
{
background: #8fc800;
color: #eee;
text-shadow: 0 -1px 1px rgba(0,0,0,0.5);
background: -webkit-linear-gradient(top, #8fc800, #438c00);
background: -moz-linear-gradient(top, #8fc800, #438c00);
background: -ms-linear-gradient(top, #8fc800, #438c00);
cursor: default;
}

和 html:

<div class="switch">
<input type="radio" name="weekly_new" id="weekSW-0" <?=$yes_checked?> value="Yes" />
<label for="weekSW-0" id="yes">ON</label>
<input type="radio" name="weekly_new" id="weekSW-1" <?=$no_checked?> value="No" />
<label for="weekSW-1" id="no">OFF</label>
</div>

和jquery:

<script>
$(".switch label:not(.checked)").click(function(){
var label = $(this);
var input = $('#' + label.attr('for'));

if (!input.prop('checked')){
label.closest('.switch').find("label").removeClass('checked');
label.addClass('checked');
input.prop('checked', true);
}
});

$(".switch input[checked=checked]").each(function(){
$("label[for=" + $(this).attr('id') + "]").addClass('checked');
});
</script>

如您所见,label.checked 为按钮提供了“外观”——我想做的是在选择“否”时使用不同的颜色,而不是选择"is"——红色表示否,绿色表示是,开创性吧?

无论如何,我这辈子都想不出是否可以做到

我为标签分配了 ID 并尝试了例如

.switch label.checked #yes { blah blah }

但这使样式变得无用!

我是这样写代码的,这样可以吗?

我已经创建了一个 fiddle ,如果更容易摆弄...... obv 必须将 valuse checked=checked 因为我通常从 mysql 表加载但它不能

http://jsfiddle.net/aS69U/

最佳答案

此 CSS 应适用于您的代码:

.switch #no.checked {
background:red;
}

Demo


顺便说一下很酷的定制 ;-)

关于jquery - CSS更改 radio 的主动选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13135942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com