如何使用 jquery 动态更改以禁用开关
How to dynamically change to disable the switchery using jquery
请问谁知道怎么换开关?
例如,我有一个父开关,如果父开关关闭,它会禁用它的子开关
该页面工作正常,但我无法弄清楚 jquery 在父开关关闭的情况下对开关进行动态更改。预先感谢您的回答
if ($(".js-switch")[0]) {
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function (html) {
var switchery = new Switchery(html, {
color: '#26B99A'
});
});
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="js-switch" name="write[33]" id="write[33]" />
<label class="onoffswitch-label" for="write[33]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
<input type="checkbox" class="js-switch" name="write[34]" id="write[34]" />
<label class="onoffswitch-label" for="write[34]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
<input type="checkbox" class="js-switch" name="write[35]" id="write[35]" />
<label class="onoffswitch-label" for="write[35]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
试试这个
$('.js-switch').each(function(idx, elm) {
new Switchery(elm, {
color: '#26B99A'
});
});
$('.js-parent').on('change', function() {
$('.js-child').each(function() {
this.click();
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="js-switch js-parent" /> Parent
<br><br>
<input type="checkbox" class="js-switch js-child" /> Child 1
<input type="checkbox" class="js-switch js-child" /> Child 2
请问谁知道怎么换开关? 例如,我有一个父开关,如果父开关关闭,它会禁用它的子开关
该页面工作正常,但我无法弄清楚 jquery 在父开关关闭的情况下对开关进行动态更改。预先感谢您的回答
if ($(".js-switch")[0]) {
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function (html) {
var switchery = new Switchery(html, {
color: '#26B99A'
});
});
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="js-switch" name="write[33]" id="write[33]" />
<label class="onoffswitch-label" for="write[33]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
<input type="checkbox" class="js-switch" name="write[34]" id="write[34]" />
<label class="onoffswitch-label" for="write[34]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
<input type="checkbox" class="js-switch" name="write[35]" id="write[35]" />
<label class="onoffswitch-label" for="write[35]">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
试试这个
$('.js-switch').each(function(idx, elm) {
new Switchery(elm, {
color: '#26B99A'
});
});
$('.js-parent').on('change', function() {
$('.js-child').each(function() {
this.click();
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="js-switch js-parent" /> Parent
<br><br>
<input type="checkbox" class="js-switch js-child" /> Child 1
<input type="checkbox" class="js-switch js-child" /> Child 2