bootstrapSwitch切换状态时不触发事件的解决方案

最近做了一个项目,表单里面有个开关按钮,如果开关 “开”则添加附加的几个表单属性,如果“关”,则隐藏附加的几个表单属性。于是选择了bootstrap中的bootstrapSwitch控件来做,因为此控件提供的方法挺多,用起来也比较方法,在加上现在bootstrap也算是比较流行的一个前端框架吧。反正最后就是选择这个控件,又不愿意或者有更好的控件的小伙伴,也欢迎推荐。

先来说说问题吧:当我写完之后,发现首次状态切换不起作用,而且只是首次切换不起作用,也不知道为什么,解决了好长时间也没解决。最后搜索了一下算是解决了。

下面来说说解决的过程吧 ,希望对各位有所帮助,不对之处,望不吝赐教。

 

首先话不多说,先来看看官网提供的一些方法。

Additional Methods

NameDescription
toggleStateToggle the switch state
toggleAnimateToggle the animate option
toggleDisabledToggle the disabled state
toggleReadonlyToggle the readonly state
toggleIndeterminateToggle the indeterminate state
toggleInverseToggle the inverse option
destroyDestroy the instance of Bootstrap Switch

Options

NameAttributeTypeDescriptionValuesDefault
statecheckedBooleanThe checkbox statetrue, falsetrue
sizedata-sizeStringThe checkbox sizenull, 'mini', 'small', 'normal', 'large'null
animatedata-animateBooleanAnimate the switchtrue, falsetrue
disableddisabledBooleanDisable statetrue, falsefalse
readonlyreadonlyBooleanReadonly statetrue, falsefalse
indeterminatedata-indeterminateBooleanIndeterminate statetrue, falsefalse
inversedata-inverseBooleanInverse switch directiontrue, falsefalse
radioAllOffdata-radio-all-offBooleanAllow this radio button to be unchecked by the usertrue, falsefalse
onColordata-on-colorStringColor of the left side of the switch'primary', 'info', 'success', 'warning', 'danger', 'default''primary'
offColordata-off-colorStringColor of the right side of the switch'primary', 'info', 'success', 'warning', 'danger', 'default''default'
onTextdata-on-textStringText of the left side of the switchString'ON'
offTextdata-off-textStringText of the right side of the switchString'OFF'
labelTextdata-label-textStringText of the center handle of the switchString' '
handleWidthdata-handle-widthString | NumberWidth of the left and right sides in pixels'auto' or Number'auto'
labelWidthdata-label-widthString | NumberWidth of the center handle in pixels'auto' or Number'auto'
baseClassdata-base-classStringGlobal class prefixString'bootstrap-switch'
wrapperClassdata-wrapper-classString | ArrayContainer element class(es)String | Array'wrapper'
onInit FunctionCallback function to execute on initializationFunction
function(event, state) {}
onSwitchChange FunctionCallback function to execute on switch state changeFunction
function(event, state) {}

 

上面最后一张图是我写的一个全局方法。

这个是我切换按钮时所触发的方法。debugger跟踪了一下发现,第一次不仅该方法。所以改了一下全局方法,如下

好了,问题解决。toggleState是切换开关状态的方法,加上之后ok.具体有什么疑问的可以留言,今天就先到这里,记得关注哦。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Calvin_it

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值