Change event on select with knockout binding, how can I know if it is a real change?

前端 未结 11 1898
独厮守ぢ
独厮守ぢ 2021-01-30 08:05

I am building a permissions UI, I have a list of permissions with a select list next to each permission. The permissions are represented by an observable array of objects which

11条回答
  •  心在旅途
    2021-01-30 08:51

    Here is a solution that may help with this strange behaviour. I couldn't find a better solution than place a button to manually trigger the change event.

    EDIT: Maybe a custom binding like this could help:

    ko.bindingHandlers.changeSelectValue = {
    
       init: function(element,valueAccessor){
    
            $(element).change(function(){
    
                var value = $(element).val();
    
                if($(element).is(":focus")){
    
                      //Do whatever you want with the new value
                }
    
            });
    
        }
      };
    

    And in your select data-bind attribute add:

    changeSelectValue: yourSelectValue
    

提交回复
热议问题