【发布时间】:2017-05-10 12:39:19
【问题描述】:
我用复选框替换了 jQuery 切换代码上的按钮,但无法使其正常工作。
这是我尝试用复选框替换按钮的 HTML 文件
index.html
<p> hi </p>
<!--<button>Press me</button>-->
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch"></label>
</div>
javascript
$(function() {
if (Cookies) {
$("p").toggle(!( !! Cookies("toggle-state")) || Cookies("toggle-state") === 'true');
}
// $('button').on('click', function()
$('#myonoffswitch.onoffswitch').is(":checked"), function()
{
$("p").toggle();
Cookies.set("toggle-state", $("p").is(':visible'), {
expires: 1,
path: '/'
});
});
});
这里有什么问题?
【问题讨论】:
-
但是,您希望输出是什么?你写了问题,但答案是什么?
-
@AlivetoDie,HTML 上的
-
@DeepakYadav,在切换时隐藏和显示
-
@Mecom - 在
$('#myonoffswitch.onoffswitch')中,onoffswitch类不存在。该元素上使用的类是onoffswitch-checkbox。所以这个函数甚至不会被调用。onoffswitch用于其父元素,但不在该元素上 -
@NikhilNanjappa,我试过你说的,但没有用。你能在我发布的小提琴上给我看吗?
标签: javascript jquery html checkbox