2015-06-22 78 views
0

我使用這個漂亮的iOS複選框插件Switchery iOS checkbox。默認情況下,該複選框處於禁用狀態。現在,如果用戶單擊具有「btn_modify」類的按鈕,並且在用戶單擊具有「btn_cancel」類的按鈕時將其禁用,則需要啓用切換盒複選框。以下是我的代碼參考。將禁用的屬性設置爲true或false switchery

<input type="checkbox" name="" value="" class="js-switch" checked /> 
<button class="btn_modify">Modify</button> 
<button class="btn_cancel">Cancel</button> 

並激活switchery

//switchery 
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch')); 
elems.forEach(function(html) { 
    var switchery = new Switchery(html, { size: 'small', disabled: true, disabledOpacity: 1.0 }); 
}); 

和我嘗試

$(".btn_modify").click(function(){ 
    $(".js-switch").attr("disabled", false); 
}); 

$(".btn_cancel").click(function(){ 
    $(".js-switch").attr("disabled", true); 
}); 

但遺憾的是沒有工作,任何幫助,想法,線索,意見,建議,將不勝感激。謝謝!

+0

您正在使用什麼版本的jQuery?你有沒有包含jQuery庫? – Abhinav

+0

我使用了最新的jquery 2.xx庫,並且我確實包含了它,並且沒有任何問題渲染切換插件 –

+0

文檔就在您鏈接到的頁面上 - 保留對該對象的引用,並調用'.disable() '和'.enable()'。 –

回答

4
var elem = document.querySelector('.js-dynamic-state'); 
var switchery = new Switchery(elem); 

document.querySelector('.js-dynamic-disable').addEventListener('click', function() { 
    switchery.disable(); 
}); 

document.querySelector('.js-dynamic-enable').addEventListener('click', function() { 
    switchery.enable(); 
}); 

你的情況,這將是

var elem = document.querySelector('.js-switch'); 
var switchery = new Switchery(elem); 

document.querySelector('.btn_cancel').addEventListener('click', function() { 
    switchery.disable(); 
}); 

document.querySelector('.btn_modify').addEventListener('click', function() { 
    switchery.enable(); 
}); 

第二個選項

CSS

.disabled{ 
opacity: 0.5; 
pointer-events: none;  
} 

JS

$(".btn_modify").on("click",function(){ 
     $(".switchery").removeClass("disabled"); 
    }); 

$(".btn_cancel").on("click",function(){ 
    $(".switchery").addClass("disabled"); 
}); 

Jsfiddle Demo

參考http://abpetkov.github.io/switchery/

+0

我看它,但無法找到一種方法來合併它與我目前的發展。 –

+0

更新了我的答案.. –

+0

0.o我以爲這是非常明顯的 – Sarfaraaz

-1

disabled = false仍將註冊爲禁用。以啓用此功能的唯一方法是完全移除已停用的屬性,是這樣的:

$(".js-switch").removeAttr('disabled'); 
+1

我嘗試過,但可悲的是不工作 –

相關問題