Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干-未在选定元素上激发的事件已更改_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript 主干-未在选定元素上激发的事件已更改

Javascript 主干-未在选定元素上激发的事件已更改,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我正在从主干生成一个下拉列表。查看 将其附加到DOM后,不会触发更改事件。delegateEvents无法修复它。有人能告诉我盲点在哪里吗 模型和集合: App.Models.DictionaryItem = Backbone.Model.extend({ default: { key: '', value: '', id: 0 } }); App.Collections.Dictionary =

我正在从
主干生成一个下拉列表。查看

将其附加到DOM后,不会触发更改事件。
delegateEvents
无法修复它。有人能告诉我盲点在哪里吗

模型和集合:

App.Models.DictionaryItem = Backbone.Model.extend({
        default: {
            key: '',
            value: '', id: 0
        }
    });

    App.Collections.Dictionary = Backbone.Collection.extend({
        model: App.Models.DictionaryItem,
        initialize: function (models, options) {

        },
        parse: function (data) {
            _.each(data, function (item) {
             //   if(item){
                var m = new App.Models.DictionaryItem({ key: item.code, value: item.name });
                this.add(m);
           // }
            }, this);
        }
    });
观点:

App.Views.ItemView = Backbone.View.extend({
        tagName: 'option',
        attributes: function () {
            return {
                value: this.model.get('key')
            }
        },
        initialize: function () {
            this.template = _.template(this.model.get('value'));
        },
        render: function () {
            this.$el.html(this.template());
            return this;
        }
    });

    App.Views.CollectionView = Backbone.View.extend({
        tagName: 'select',
        attributes: {
            'class': 'rangesList'
        },
        events: {
            'change .rangesList': 'onRangeChanged'
        },
        initialize: function (coll) {
            this.collection = coll;
        },
        render: function () {
            _.each(this.collection.models, function (item) {
                this.$el.append(new App.Views.ItemView({ model: item }).render().el);
            }, this);
           // this.delegateEvents(this.events);
            return this;
        },
        selected: function () {
            return this.$el.val();
        },
        onRangeChanged: function () {
            alert('changed');
        }
    });
渲染:

var coll = new App.Collections.Dictionary(someData, { parse: true });
var v= new App.Views.CollectionView(coll);
var vv=v.render().el;

//   new App.Views.CollectionView(coll).render().el;
$('body').append(vv)

集合视图上的
标记名
属性

tagName: 'select',
attributes: {
    'class': 'rangesList'
},
假设
el
将是
。但是您的
事件

events: {
    'change .rangesList': 'onRangeChanged'
},
正在从视图的
el
中的
.rangesList
侦听
'change'
事件。从:

事件以
{“事件选择器”:“回调”}
格式写入。[…]省略选择器会导致事件绑定到视图的根元素(
this.el

因此,你试图从一些不存在的事物中倾听事件。如果要直接从视图的
el
中侦听事件,请省去选择器:

events: {
    'change': 'onRangeChanged'
}

this.template=\.template(this.model.get('value')从这一行判断,您似乎不了解JS模板是如何工作的。或者您真的将JS模板存储在模型中吗?