如何防止在设置 Bootstrap Switch 状态时触发更改事件?

huangapple go评论63阅读模式
英文:

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() {
  $(&quot;#IsPublish&quot;).bootstrapSwitch();
  $(&quot;#IsPublish&quot;).bootstrapSwitch(&#39;state&#39;, true);
});

$(&#39;#IsPublish&#39;).on(&#39;click&#39;, function(event) {
  $(&#39;#status&#39;).text(&#39;click&#39;);
});

$(&#39;#IsPublish&#39;).on(&#39;switchChange.bootstrapSwitch&#39;, function(event, state) {
  //$(&#39;#status&#39;).text(&#39;switchChange&#39;);  
});

$(&#39;#IsPublish&#39;).change(&#39;switchChange.bootstrapSwitch&#39;, function(event, state) {
  //$(&#39;#status&#39;).text(&#39;change&#39;); 
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css&quot; integrity=&quot;sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==&quot; crossorigin=&quot;anonymous&quot;
  referrerpolicy=&quot;no-referrer&quot; /&gt;
&lt;link href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;input type=&quot;checkbox&quot; id=&quot;IsPublish&quot; /&gt;
&lt;br&gt;&lt;br&gt;
&lt;label id=&quot;status&quot;&gt;&lt;/label&gt;

<!-- 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() {
  $(&quot;#IsPublish&quot;).bootstrapSwitch({
    &#39;state&#39;: true
  });
});

$(&#39;#IsPublish&#39;).on(&#39;click&#39;, function(event) {
  $(&#39;#status&#39;).text(&#39;click&#39;);
});

$(&#39;#IsPublish&#39;).on(&#39;switchChange.bootstrapSwitch&#39;, function(event, state) {
  console.log(&#39;changed in on()&#39;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css&quot; integrity=&quot;sha512-lANJTDwF23Xos7T5Q7SNbZ7dFxLG8YSQJ2qCGrav0Fhe5vns60+tUECZv+9udXdKiX3/YTWt++tEKyEcuZv4dQ==&quot; crossorigin=&quot;anonymous&quot;
  referrerpolicy=&quot;no-referrer&quot; /&gt;
&lt;link href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;input type=&quot;checkbox&quot; id=&quot;IsPublish&quot; /&gt;
&lt;br&gt;&lt;br&gt;
&lt;label id=&quot;status&quot;&gt;&lt;/label&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月7日 01:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定