admin管理员组文章数量:1429575
Firstly, here is a JsFiddle: /
This is what I want to happen:
- Switch starts in the indeterminate / in-between state (this works)
- Event fires when user chooses either state (this works)
- When use clicks RESET the switch goes back to the indeterminate / in-between state (this works)
- Event fires when user chooses either state (this DOES NOT work)
What is happening is if the user goes back to whatever the previous state was then no event fires. So say it was on YES, they click RESET, then click YES again - no event fires (but if it was previously on YES, and the reset then went to NO then the event would fire).
I've got no idea how to approach this. I've tried things like destroying and recreating the switch:
$('input[name=test]').bootstrapSwitch('destroy');
$('input[name=test]').bootstrapSwitch();
To no avail unfortunately.
Here is the important thing for me - all I need to know is when the user moves from the indeterminate state to ANY state. I don't actually care what they choose, as long as they choose something. So if anyone can think of a different type of workaround with that in mind (maybe it makes it easier) that would definitely be wele.
Any help is appreciated, this is driving me crazy.
Firstly, here is a JsFiddle: http://jsfiddle/VTv2j/76/
This is what I want to happen:
- Switch starts in the indeterminate / in-between state (this works)
- Event fires when user chooses either state (this works)
- When use clicks RESET the switch goes back to the indeterminate / in-between state (this works)
- Event fires when user chooses either state (this DOES NOT work)
What is happening is if the user goes back to whatever the previous state was then no event fires. So say it was on YES, they click RESET, then click YES again - no event fires (but if it was previously on YES, and the reset then went to NO then the event would fire).
I've got no idea how to approach this. I've tried things like destroying and recreating the switch:
$('input[name=test]').bootstrapSwitch('destroy');
$('input[name=test]').bootstrapSwitch();
To no avail unfortunately.
Here is the important thing for me - all I need to know is when the user moves from the indeterminate state to ANY state. I don't actually care what they choose, as long as they choose something. So if anyone can think of a different type of workaround with that in mind (maybe it makes it easier) that would definitely be wele.
Any help is appreciated, this is driving me crazy.
Share Improve this question asked Apr 9, 2015 at 6:33 b85411b85411 10.1k15 gold badges71 silver badges126 bronze badges 1- Thanks to Uhkis (a legend) for helping me with this. He worked out I needed to rebind the event after destroying - see jsfiddle/VTv2j/77 – b85411 Commented Apr 9, 2015 at 6:47
2 Answers
Reset to default 3Here's a working version of the code above: http://jsfiddle/byzwng4t/1/
function initializeSwitch(selector) {
$(selector).bootstrapSwitch('destroy', true);
$(selector).bootstrapSwitch({
'state': null
});
$(selector).on('switchChange.bootstrapSwitch', function(event, state) {
console.log('change event' + (new Date()).getTime());
});
}
$(function() {
initializeSwitch('input[name=test]');
$('.reset').click(function() {
initializeSwitch('input[name=test]');
});
});
The trick is to bind the events again after destroying the switch.
$('input[name=test]').bootstrapSwitch('state', $('input[name=test]').is(':checked'), true);
Refer: http://bootstrapswitch.site/methods.html
Stackoverflow: Bootstrap Switch - Change state after clicking on button
本文标签: javascriptReset bootstrap switch stateStack Overflow
版权声明:本文标题:javascript - Reset bootstrap switch state - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467136a2659581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论