2017-11-25 60 views
-1

我希望禁用繼續按鈕,當選中li時,如選中,並啓用當我取消選中時,我使用simscheckbox lib,使li可檢查。我把這個庫放在上面的html代碼中,但是如何禁用並啓用這個功能,當我點擊li項目任何和它的禁用和啓用,當我再次點擊它的啓用,plzz幫助我......我的lib運行「T運行這個堆棧,但我對我的conputer運行,所以我告訴李是可檢查的項目..你可以看到simscheckbox js文件我把HTML代碼.... PLZ幫助..禁用並啓用li上的按鈕選擇

(function($) { 
 

 
    $('.check').change(function() { 
 
    if ($('.check:checked').length) { 
 
     $('.btn_check').removeAttr('disabled'); 
 
    } else { 
 
     $('.btn_check').attr('disabled', 'disabled'); 
 
    } 
 
}); 
 

 

 
}(jQuery));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://drive.google.com/file/d/1QoDVNlXUfTkKgzvzuIYRqldynG-N0Bbo/view?usp=sharing.js"></script> 
 
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
 
<div class="quiz" id="quiz" data-toggle="buttons"> 
 
    <ul class="demo" id="sn-list"> 
 
     <li class="input-lg check">Pepperoni</li> 
 
     <li class="input-lg check">Mushrooms</li> 
 
     <li class="input-lg check">Anchovies</li> 
 
     <li class="input-lg check">Sausage</li> 
 
     <li class="input-lg check">Artichoke hearts</li> 
 
    </ul> 
 
    <button type="submit" class="btn btn-success btn_check">Continue 
 
    \t </button> 
 
</div>

回答

0

對於將來的問題,請明確提到您使用的插件(提供鏈接!)...在這種情況下,哪個是simsCheckbox

爲了您的HTML標記,我只是添加了disabled屬性。

現在,disabled是屬性,而不是屬性。
所以你必須使用$('.btn_check').prop('disabled', [true/false]);來改變它使用jQuery。

您還必須使用.simsCheckbox()實例化該插件,如在GitHub頁面中清楚說明的那樣。

現在...你只是想啓用/禁用一個按鈕...至少基於一個li檢查。

正如您將在下面看到的,在click上,它切換了checked類。
因此,如果至少有一個.check也具有.checked,則啓用該按鈕。


對不起所有,但我不得不復制粘貼GitHub上的插件腳本,因爲沒有可用的CDN。

方式低於的解決方案。

// See way below for my solution... 
 

 
// ======== simscheckbox copy pasted form: https://raw.githubusercontent.com/simsek97/simsCheckbox/master/simsCheckbox.js 
 

 
/*! 
 
* SmartClass Checkbox plugin 
 
* =================================== 
 
* 
 
* developed by Mert Simsek ([email protected]) 
 
* for SmartClass Project [www.smartclass.us] 
 
* ------------------------- 
 
* @usage $("#element").simsCheckbox(); 
 
*/ 
 
    
 
(function($) { 
 

 
    //vars 
 
    var _mobile = /ipad|iphone|ipod|android|blackberry|windows phone|opera mini|silk/i.test(navigator.userAgent); 
 

 
    //plugin init 
 
    $.fn.simsCheckbox = function(options) { 
 
    
 
     var selectorElt = this, 
 
      checkboxClass = "btn btn-block btn-social"; 
 
    
 
     //settings 
 
     var settings = $.extend({ 
 
    
 
      //style 
 
      //set treat as checkbox or radio 
 
      btnStyle: 'checkbox', 
 
      
 
      //selector height 
 
      height: 'auto', 
 
      
 
      //element 
 
      element: "li", 
 
     
 
      //title icon 
 
      titleIcon: "square-o", 
 
    
 
      //unchecked class 
 
      uncheckedClass: "btn-default", 
 
     
 
      //checked class 
 
      checkedClass: "btn-warning", 
 
      
 
      //select/unselect all button 
 
      selectAllBtn: false, 
 
    
 
      //select/unselect text 
 
      selectAllText: 'Select/Unselect All', 
 
      
 
      //callback fn ifChecked 
 
      ifChecked: function() {}, 
 

 
      //callback fn ifUnChecked 
 
      ifUnChecked: function() {}, 
 

 
      //callback fn ifToggled 
 
      ifToggled: function() {}, 
 
    
 
     }, options); 
 

 
     return selectorElt.each(function(){ 
 
     
 
      //set some css for the selector 
 
      selectorElt.css({'margin': '0', 'padding': '0'}); 
 
      
 
      //set the height of the selector first 
 
      if(settings.height == 'auto') selectorElt.css('height', 'auto'); 
 
      else selectorElt.css({'height': settings.height, 'overflow': 'auto'}); 
 
     
 
      //add an identifier class to the elements 
 
      selectorElt.find(settings.element).addClass('sims-selectable'); 
 
      
 
      //get elements and handle 
 
      selectorElt.find(settings.element).each(function(i) { 
 
       
 
       var simsElement=$(this), simsElementTitle=simsElement.html(); 
 
       
 
       //add checkbox class 
 
       simsElement.addClass(checkboxClass); 
 
       
 
       //add checked or unchecked class 
 
       if(simsElement.hasClass('checked')) simsElement.addClass(settings.checkedClass).html('<i class="fa fa-fw fa-check-' + settings.titleIcon + '"></i> ' + simsElementTitle); 
 
       else simsElement.addClass(settings.uncheckedClass).html('<i class="fa fa-fw fa-' + settings.titleIcon + '"></i> ' + simsElementTitle); 
 
       
 
       //set click event if it is not disabled 
 
       simsElement.off('click').on('click', function (e) { 
 
    
 
        e.preventDefault(); 
 
        
 
        //if the element is disabled then do nothing 
 
        if($(this).hasClass('disabled')) return false; 
 
        
 
        //if the style is radio kind then unselect all first 
 
        if(settings.btnStyle == 'radio') 
 
        { 
 
         selectorElt.find(settings.element).addClass(settings.uncheckedClass).removeClass(settings.checkedClass).find('i').addClass('fa-' + settings.titleIcon).removeClass('fa-check-' + settings.titleIcon); 
 
        } 
 
        
 
        //toggle the item 
 
        $(this).toggleClass(settings.uncheckedClass).toggleClass(settings.checkedClass).find('i').toggleClass('fa-' + settings.titleIcon).toggleClass('fa-check-' + settings.titleIcon); 
 

 
        //callback 
 
        clickCheckbox($(this)); 
 
        
 
       }); //end simsElement click 
 
       
 
      }); //end selectorElt each 
 
      
 
      //set checkAll button 
 
      if(settings.selectAllBtn) 
 
      { 
 
       
 
       //test all checked or not 
 
       var allChecked = (selectorElt.find(settings.element).length == selectorElt.find(settings.element + '.checked').length) ? true : false; 
 
       var selectAllBtnContainer = $('<ul></ul>').css({'margin': '5px 0 0 0', 'padding': '0'}); 
 
       var selectAllBtnElt = $('<' + settings.element + ' class="sims-btn-select-all"></' + settings.element + '>').css('border', '1px dashed').addClass(checkboxClass + ' ' + (allChecked ? settings.checkedClass : settings.uncheckedClass)).html('<i class="fa fa-fw fa-' + (allChecked ? 'check-' : '') + settings.titleIcon + '"></i> ' + settings.selectAllText); 
 
       
 
       selectorElt.after(selectAllBtnContainer.append(selectAllBtnElt)); 
 
       
 
       //set click event for the selectAll button 
 
       selectAllBtnContainer.find('.sims-btn-select-all').off('click').on('click', function (e) { 
 
        
 
        //prevent default events 
 
        e.preventDefault(); 
 
        
 
        //get button 
 
        var thisButton = $(this); 
 
        
 
        //toggle the item 
 
        thisButton.toggleClass(settings.uncheckedClass).toggleClass(settings.checkedClass).find('i').toggleClass('fa-' + settings.titleIcon).toggleClass('fa-check-' + settings.titleIcon); 
 
        
 
        //if all items are selected then select-all button is checked 
 
        //if one of the items is unselected then select-all button is unchecked 
 
        selectorElt.find(settings.element + '.sims-selectable:not(.disabled)').each(function(){ 
 
         
 
         //fix classes of the items 
 
         if(thisButton.hasClass("btn-default")) $(this).removeClass(settings.uncheckedClass).addClass(settings.checkedClass).find('i').removeClass('fa-' + settings.titleIcon).addClass('fa-check-' + settings.titleIcon); 
 
         else $(this).addClass(settings.uncheckedClass).removeClass(settings.checkedClass).find('i').addClass('fa-' + settings.titleIcon).removeClass('fa-check-' + settings.titleIcon); 
 
         
 
         //trigger click event for the items 
 
         $(this).trigger("click"); 
 
        
 
        }); 
 
       
 
       }); 
 
       
 
      } //end if 
 
     
 
      function clickCheckbox(item) { 
 
       
 
       //check if the button checked or unchecked 
 
       //then call function properly 
 
       if(item.hasClass(settings.checkedClass)) settings.ifChecked.call(item); 
 
       else settings.ifUnChecked.call(item); 
 
       
 
       //call toggle function anyways 
 
       settings.ifToggled.call(item); 
 
       
 
      } 
 
      
 
     }); //end return 
 
     
 
    } //end function 
 
    
 
}(jQuery)); 
 

 
// =========================================================== 
 

 
// My solution for OP is here 
 

 
$(document).ready(function(){ 
 
    
 
    // INSTANTIATE the plugin!!! 
 
    
 
    // That was copy/pasted form: https://github.com/simsek97/simsCheckbox 
 
    $(".demo").simsCheckbox({ 
 
     /* 
 
     ifChecked: function() { 
 
      console.log('checked'); 
 
     }, 
 
     ifUnchecked: function() { 
 
      console.log('unchecked'); 
 
     }, 
 
     ifToggled: function() { 
 
      console.log('toggled'); 
 
     } 
 
     */ 
 
     
 
     // Here is the place for some callbacks on each click. 
 
    }); 
 
    
 
    
 
    
 
    // And that is what you want: 
 
    
 
    $('.check').on("click",function() { 
 
     $(this).toggleClass("checked"); 
 

 
     if ($(".check.checked").length>0) { 
 
     $('.btn_check').prop('disabled', false); 
 
     } else { 
 
     $('.btn_check').prop('disabled', true); 
 
     } 
 
    }); 
 
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div class="quiz" id="quiz" data-toggle="buttons"> 
 
    <ul class="demo" id="sn-list"> 
 
     <li class="input-lg check">Pepperoni</li> 
 
     <li class="input-lg check">Mushrooms</li> 
 
     <li class="input-lg check">Anchovies</li> 
 
     <li class="input-lg check">Sausage</li> 
 
     <li class="input-lg check">Artichoke hearts</li> 
 
    </ul> 
 
    <button type="submit" class="btn btn-success btn_check" disabled>Continue</button> 
 
</div>

+0

Louys帕特里斯·貝塞特感謝.. –

+0

其工作thanksssss –