英文:
How can I prevent the change event from firing when setting state in Bootstrap Switch?
问题
在我的document.ready
函数中,我将此控件设置为bootstrapSwitch
,并根据来自数据库的数据设置状态。设置状态会触发change switchChange.bootstrapSwitch
事件。有没有办法让它不触发事件?我尝试将它更改为点击事件,但那不起作用。任何帮助都将是很好的,我只希望在用户选择控件时触发点击事件。
$(document).ready(function() {
$("#IsPublish").bootstrapSwitch();
$("#IsPublish").bootstrapSwitch('state', true);
});
$('#IsPublish').on('click', function(event) {
$('#status').text('click');
});
$('#IsPublish').on('switchChange.bootstrapSwitch', function(event, state) {
//$('#status').text('switchChange');
});
$('#IsPublish').change('switchChange.bootstrapSwitch', function(event, state) {
//$('#status').text('change');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css" integrity="sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<input type="checkbox" id="IsPublish" />
<br><br>
<label id="status"></label>
英文:
On my document.ready I am setting this control to a bootstrapSwitch
and setting the state based on data from a database. Setting that state fires off the change switchChange.bootstrapSwitch
. Is there anyway I can set it so it will not fire off a event? I have tried to change it to a click event but that is not working. Any help would be great I just want the click event when the users selects the control.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$("#IsPublish").bootstrapSwitch();
$("#IsPublish").bootstrapSwitch('state', true);
});
$('#IsPublish').on('click', function(event) {
$('#status').text('click');
});
$('#IsPublish').on('switchChange.bootstrapSwitch', function(event, state) {
//$('#status').text('switchChange');
});
$('#IsPublish').change('switchChange.bootstrapSwitch', function(event, state) {
//$('#status').text('change');
})
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css" integrity="sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==" crossorigin="anonymous"
referrerpolicy="no-referrer" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<input type="checkbox" id="IsPublish" />
<br><br>
<label id="status"></label>
<!-- end snippet -->
答案1
得分: 3
你正在询问关于你提出的解决方案而不是你的问题的 xy问题。你应该询问如何在初始化时设置状态。你可以使用 state
选项属性来实现这一点,它会消除你之前使用的后续状态更新中的事件。
$(document).ready(function() {
$("#IsPublish").bootstrapSwitch({
'state': true
});
});
$('#IsPublish').on('click', function(event) {
$('#status').text('click');
});
$('#IsPublish').on('switchChange.bootstrapSwitch', function(event, state) {
console.log('changed in on()');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css" integrity="sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==" crossorigin="anonymous"
referrerpolicy="no-referrer" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<input type="checkbox" id="IsPublish" />
<br><br>
<label id="status"></label>
英文:
You're asking an xy question, about your proposed solution instead of your problem. You should be asking how to set state on initialization. You can do that with the state
option property, which eliminates the event in the later state update you had been using.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$("#IsPublish").bootstrapSwitch({
'state': true
});
});
$('#IsPublish').on('click', function(event) {
$('#status').text('click');
});
$('#IsPublish').on('switchChange.bootstrapSwitch', function(event, state) {
console.log('changed in on()');
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css" integrity="sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==" crossorigin="anonymous"
referrerpolicy="no-referrer" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<input type="checkbox" id="IsPublish" />
<br><br>
<label id="status"></label>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论