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

时间:2015-09-12 14:40:49

标签: jquery twitter-bootstrap-3 bootstrap-switch

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

<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>

关于js文件的文档准备,我做

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

并附加在php页面

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

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

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

启用/禁用引导切换的jQuery命令是什么?谢谢!

2 个答案:

答案 0 :(得分:31)

您可以通过以下多种方式完成此操作:

<强> DEMO

  

使用选项

输入1

初始化

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

类型2

初始化后

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

使用方法

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

<强> Documentation

答案 1 :(得分:0)

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

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

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

用法:

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