我需要在点击按钮事件时使用jquery禁用切换复选框。
我知道有一个disable()
方法,但我不知道如何在函数内部使用它导致问题是我首先要检索所有复选框的初始化和我不知道怎么做。< / p>
我的jsfiddle示例:https://jsfiddle.net/g94gkaL7/38/
答案 0 :(得分:2)
保持对创建的切换项的引用,然后遍历它们并调用disable。
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
var switcheries= [];
elems.forEach(function(html, index) {
var switchery = new Switchery(html,{size: 'small',});
switcheries[index] = switchery;
});
$("#dis").on("click", function(){
$("input[type='checkbox']").attr("disabled", true); // works for standard input checkbox only
// now i want to retrieve all existing swithcery to disable them
//switchery.disable();
switcheries.forEach(item => item.disable());
});
});
答案 1 :(得分:0)
var switcherylist = [];
$(document).ready(function() {
//switchery
//var checkbox = document.querySelector('.js-switch');
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function(html) {
var switchery = new Switchery(html,{size: 'small',});
switcherylist.push(switchery);
});
$("#dis").on("click", function(){
$("input[type='checkbox']").attr("disabled", true); // works for standard input checkbox only
switcherylist.forEach(function(switchery) {
switchery.disable();
});
// now i want to retrieve all existing swithcery to disable them
//switchery.disable();
});
});