在Knockout中将禁用的类添加到复选框

时间:2017-11-09 20:08:46

标签: javascript knockout.js

如果这个问题很愚蠢,请提前道歉。我正在尝试检查PHP权限,如果它们返回为false,则在Knockout中禁用复选框(或向其中添加“禁用”类)。但是,没有太多运气。我的代码。感谢

  ax.ko.UserProfileModel = function(data) {
   this.display_name = data.display_name;
   this.enabled = data.enabled;
   this.first_name = data.first_name;
   this.gender = data.gender;
   this.sales_rep_code = data.sales_rep_code;
   this.billable = data.billable;
} 


//VM
ax.ko.UserProfilesVM = function(data) {
var self = this;

 self.userProfile = ko.observableArray([]);
 self.userProfiles = ko.observableArray([]);
 self.isLoading = ko.observable(false);
 self.viewCreateEdit = ko.observable('');
 self.carouselSettings = ax.Carousel.defaults;
 self.canEditAccount = ko.observable();
 self.isBillable = ko.observable();

self.populateUserProfiles = function(data) {
self.userProfiles($.map(data,function(i) { return new 
ax.ko.UserProfileModel(i); }));
};

self.populateUserProfile = function(data) {
 self.userProfile(new ax.ko.UserProfileModel(data));
};
};


<? if ($perms['user_profiles|edit_billable']===FALSE) { ?>
        <div class="field">
        <label><?=l(273)?></label>
        <div class="input">
          <input type="checkbox" name="billable" value="1" data-bind="checked: userProfile().billable, disable:isBillable();">
        </div>
      </div>
    </div>

<? } ?>

1 个答案:

答案 0 :(得分:2)

试试这个,更像是你可以按照@Jason的建议为你的绑定添加禁用http://knockoutjs.com/documentation/disable-binding.html

&#13;
&#13;
MyVM = function(condition){
    that = this;
    that.flag = ko.observable(true);
    that.condition = ko.observable(condition);
}

ko.applyBindings(new MyVM(true));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>


<div>

<input type="checkbox" data-bind="checked: flag, disable: condition"/> 

</div>
&#13;
&#13;
&#13;