简体   繁体   English

jQuery不适用于!text插件需要具有主干的js

[英]Jquery didn't work with !text plugin require js with backbone

I created a template file home.html for my home view with a form in it : 我为主页视图创建了一个模板文件home.html ,其中包含一个表单:

<form class="adform">
   <input type="button" id="addcompareitem" class="addcompareitem" value="Add compare item" />
</form>

This is some code of my home view : 这是我的主视图的一些代码:

var HomeView = Backbone.View.extend({
  initialize: function() {
     item1.disableCompareBtn();
  },
  render : function(){
     this.$el.html(homeTemplate);
  }
});
return HomeView;

Here's disableCompareBtn method : 这是disableCompareBtn方法:

this.disableCompareBtn = function(){
    if(compareitems == null || compareitems.length < 2){
        $('#addcompareitem').prop('disabled', true);
        console.log(1);
    }

};

Problem : when home view initialize, the button still enable, but the console.log(1) is working. 问题:初始化主视图时,该按钮仍然启用,但是console.log(1)正在工作。

Any idea what caused this. 不知道是什么原因造成的。 Thanks. 谢谢。

Looks like you're running the disable before the HTML even exists. 看起来您在HTML甚至还没有存在之前就在运行禁用功能。 My bet is that if you console.log($('#addcompareitem')) you'll get no elements returned to you. 我敢打赌,如果您console.log($('#addcompareitem'))您将不会得到任何返回的元素。 Instead, try disabling the buttons immediately after running this.$el.html(homeTemplate) . 相反,请尝试在运行this.$el.html(homeTemplate)之后立即禁用按钮。

Just for clarity's sake, initialize is called when something is constructed, and then you call render on the constructed object. 只是为了清楚起见,在构造某些对象时调用initialize,然后在构造的对象上调用render。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM