使用 jquery 禁用和启用引导程序开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32540041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:53:46  来源:igfitidea点击:

disabling and enabling bootstrap switch using jquery

jquerytwitter-bootstrap-3bootstrap-switch

提问by user4826347

I am using bootstrap switch and currently got stuck as there aren't any strong documentation or examples that I could get access to.

我正在使用引导程序开关,目前卡住了,因为没有任何我可以访问的强大文档或示例。

<div class="col-xs-6">
  <div class="pull-left">
     <input class="switch" type="checkbox" id="cb_project_status"  name="cb_project_status" 
        <?php if($data_p_projectstatus=="ACTIVE") echo("checked"); else echo("");?> 
        data-on-color="success" data-off-text="Inactive" data-on-text="Active" 
        data-off-color="warning" data-size="mini" disabled>
     </div> 
 </div>

On document ready of js file, I do

在准备好 js 文件的文档时,我做

$("#cb_project_status").bootstrapSwitch();

and have attached in in the php page

并已附加在 php 页面中

 <link rel="stylesheet" href="css/bootstrap-switch.min.css">

I need to achieve the following functionality: Unless user enters all mandatory fields, the switch button needs to be disabled.

我需要实现以下功能:除非用户输入所有必填字段,否则需要禁用切换按钮。

if($.fn.validateFormInputs()){
   // enable bootstrap switch
}else{
   // disable bootstrap switch
}

What is the jQuery command to enable/disable bootstrap switch. Thanks!

启用/禁用引导程序开关的 jQuery 命令是什么。谢谢!

回答by Guruprasad Rao

You can do it in many ways as below:

您可以通过以下多种方式进行操作:

DEMO

演示

Using options

使用选项

Type 1

类型 1

With initialization

带初始化

$("[name='my-checkbox']").bootstrapSwitch({
    disabled:true
});

Type 2

类型 2

After initialization

初始化后

$("[name='my-checkbox']").bootstrapSwitch(); //initialized somewhere
$("[name='my-checkbox']").bootstrapSwitch('disabled',true);

Using Method

使用方法

$("[name='my-checkbox']").bootstrapSwitch(); //initialized somewhere
$("[name='my-checkbox']").bootstrapSwitch('toggleDisabled',true,true);

Documentation

文档

回答by Jeremy Lynch

If you enableand disablea lot you can also write your own methods:

如果你enabledisable很多你也可以编写自己的方法:

$.fn.disable_switch = function() {
  this.bootstrapSwitch('toggleDisabled',true,true);
};

$.fn.enable_switch = function() {
  this.bootstrapSwitch('toggleDisabled',true,true);
};

Usage:

用法:

$("[name='my-checkbox']").disable_switch()