oncheck复选框显示主干中的数据

oncheck checkbox show data in backbone

本文关键字:数据 复选框 显示 oncheck      更新时间:2023-09-26

我为主视图创建了一个模板文件home.html,其中有一个复选框:

<input type="checkbox" id="chkcompareitem" class="chkcompareitem" />
<input type="button" id="compareitem" class="compareitem" value="Compare items" />

这里的观点:

var HomeView = Backbone.View.extend({
   initialize: function() {
   },
   events : {
    'click #chkcompareitem' : 'addItemtoCompareLS'
   },
   addItemtoCompareLS : function(e){
      if(e.currentTarget.checked){
           item1.addnewitem(newItem);
           item1.disableCompareBtn();
      }
   },
   render : function(){
     this.$el.html(homeTemplate);
     item1.disableCompareBtn();
   }
});
return HomeView;

addnewitem方法用于将新项添加到localStorage。

此处disableCompareBtn():

if(compareitems == null || compareitems.length < 2){
  $('#compareitem').prop('disabled', true);
}else{
  $('#compareitem').prop('disabled', false);
}

问题:当我选中复选框时,Item被添加到localStorage,但按钮仍然被禁用,即使localStorage的长度>2。

也许你可以用这个来代替道具。

$('#compareitem').attr("disabled","disabled);//用于添加属性禁用

以及$('#compareitem').removeAttr("已禁用");//用于删除禁用属性的