簡體   English   中英

切換后如何刪除復選框上的jQuery事件監聽器?

[英]How to remove jQuery event listener on checkbox after it was toggled?

我有以下jQuery代碼

$('#filterPageSearchModel_CarMakeFilter').on('change', function () {         
         var make = $(this).val();        
         CreateList(make, '@actionModel', '@idModel');
         CreateList(make, '@actionEngineSize', '@idEngineSize');
         CreateList(make, '@actionSubModel', '@idSubModel');
         CreateList(make, '@actionYear', '@idYear');
         CreateList(make, '@actionMarkOrSeries', '@idMarkOrSeries');         
     });

 var CreateList = function (make, action, id, model, engineSize, submodel) {
        $('.loader').css('display', 'inline');
      //  $(DropDownObject).prop("disabled", true);
        var Make = make;
        var Model = model;
        var Id = id;
        var EngineSize = engineSize;
        var SubModel = submodel;
        $.ajax({
            url: action,
            type: 'GET',
            datatype: 'JSON',
            data: { Make: Make, Model: Model, EngineSize: EngineSize, SubModel: SubModel },
            success: function (result) {
                $(Id).html('');
                $(Id).append($('<option>Select an Option</option>'));
                $.each(result, function (i, item) {
                    $(Id).append($('<option></option>').val(item.Value).html(item.Text));
                });
                $(Id).trigger("chosen:updated");
                $(Id).val(""); // set value to null
                $('.loader').css('display', 'none');
                //$(DropDownObject).prop("disabled", false);
            }
        });
    };

我想在勾選復選框時禁用功能CreateList或以某種方式隱藏/禁用“ #filterPageSearchModel_CarMakeFilter”的事件“ onchange”,並且顯然當未勾選復選框時,再次啟用這些事件。

請嘗試以下方法:

$('#filterPageSearchModel_CarMakeFilter').on('change', function (){
    if(!this.checked) {
        var make = $(this).val();        
        CreateList(make, '@actionModel', '@idModel');
        CreateList(make, '@actionEngineSize', '@idEngineSize');
        CreateList(make, '@actionSubModel', '@idSubModel');
        CreateList(make, '@actionYear', '@idYear');
        CreateList(make, '@actionMarkOrSeries', '@idMarkOrSeries'); 
    }
});

有一個復選框還是多個復選框? 是'filterPageSearchModel_CarMakeFilter'復選框或另一個復選框對此產生了影響?

只需像這樣獲取復選框值... $('#check_id').val();

所以這個

$('#filterPageSearchModel_CarMakeFilter').on('change', function () {
  if ($('#check_id').val()) {    // if checkbox is checked, run the bottom code    
    var make = $(this).val();        
    ...  
  }       
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM