我有这个开关,当点击将改变下一个隐藏值为0或1,但我需要它设置正确的图标基于加载的价值,但不能解决。jQuery开关没有设置默认图标,但显示两个
// Enable/Disable feature columns
$('[id^=PC_Row]').each(function() {
var $featureStatuses = $(this);
if ($featureStatuses == "1") {
$featureStatuses.prev('.inactive').hide();
} else {
$featureStatuses.prev('.active').hide();
}
});
$('.switch').click(function() {
$(this).find('.inactive, .active').toggle();
var featureStatus = $(this).find('[id^=PC_Row]').val();
//console.log(featureStatus);
if (featureStatus == "1") {
$(this).find('[id^=PC_Row]').val('0');
} else {
$(this).find('[id^=PC_Row]').val('1');
}
});
.switch-wrapper {
text-align: center;
}
.switch {
font-size: 1.5em;
margin: 0 auto;
position: relative;
height: 50px;
width: 25px;
}
.switch i {
position: absolute;
top: 14px;
left: 0;
}
.switch .active {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" media="all">
<div class="switch-wrapper">
<div class="switch">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="PC_Row_1_Feature_1_Enabled" name="PC_Row_1_Feature_1_Enabled" value="1">
</div>
</div>
<div class="switch-wrapper">
<div class="switch">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="PC_Row_1_Feature_1_Enabled" name="PC_Row_1_Feature_1_Enabled" value="1">
</div>
</div>
<div class="switch-wrapper">
<div class="switch">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="PC_Row_1_Feature_1_Enabled" name="PC_Row_1_Feature_1_Enabled" value="0">
</div>
</div>
<div class="switch-wrapper">
<div class="switch">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="PC_Row_1_Feature_1_Enabled" name="PC_Row_1_Feature_1_Enabled" value="0">
</div>
</div>
我可以建议你不'JavaScript'都更好的解决方案? –
当然,我有很多PHP控件的东西,但一定要请建议,原因使用JS和更好的支持跨浏览器。 – James