简体   繁体   English

将复选框转换为切换按钮

[英]Convert checkbox to toggle button

I want to change checkbox to toggle button? 我要更改复选框以切换按钮吗? There are no label tags in my input tags and I cant add them manually either, because I dont have access to core files. 输入标签中没有标签标签,我也无法手动添加它们,因为我无权访问核心文件。 I can add jquery / script and or css to my files via CMS. 我可以通过CMS将jquery /脚本和/或CSS添加到我的文件中。

Here is the code i have for checkbox 这是我的复选框代码

<input type="Checkbox" name="savetheocean">

Is there any solution? 有什么解决办法吗?

basically this is how you can turn it to a toggle button: 基本上,这是将其转换为切换按钮的方法:

$('input[name="savetheocean"]').click(function(){
    if($(this).is(':checked')){
        //do something
    }
    else{
       //undo something
    }
});

UPDATE: 更新:

you can see a working Demo here: http://jsfiddle.net/b4C7G/ 您可以在此处查看有效的演示: http : //jsfiddle.net/b4C7G/

http://i.stack.imgur.com/NmgLm.png

JSFiddle: JSFiddle:

link 链接

CSS: CSS:

a.toggler.on {
  background: green;
  cursor: pointer;
  border: 2px solid black;
  border-right-width: 15px;
  padding: 0 5px;
  border-radius: 5px;
}

a.toggler.off {
  background: red;
  border-right-width: 2px;
  border-left-width: 15px;
}

HTML 的HTML

<a href="#" class="toggler">&nbsp;</a>

jQuery jQuery的

$(function () {
    $('input[name="toggle"]').change(function () {
        $("a.toggler").toggleClass("off", this.checked)
    }).change();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM