简体   繁体   English

主干点击事件未在 Google Chrome 中触发

[英]Backbone click event not firing in Google Chrome

I have the following code and it works in every browser except Google Chrome.我有以下代码,它适用于除 Google Chrome 之外的所有浏览器。 I want every time I click on an option to alert me with the "You selected option (selected option number)" text.我希望每次单击一个选项时都用“您选择的选项(选定的选项编号)”文本提醒我。 I cannot make it work.我不能让它工作。

var MyView = Backbone.View.extend({

    el : "#eventPage",

    initialize : function(options) {
        _.bindAll(this, 'render', 'changeNum');
        this.render();
    },

    render : function(){
        var a = [];
        var b = 0;

        a[b++] = '<div>';
        a[b++] = '  <select id="select">';
        a[b++] = '      <option class="sel" id="1">ONE</option>';
        a[b++] = '      <option class="sel" id="2">TWO</option>';
        a[b++] = '      <option class="sel" id="3">THREE</option>';
        a[b++] = '  </select>';
        a[b++] = '</div>';

        $(this.el).html(a.join(""));
    },

    events : {
        "click #select .sel":"changeNum"
    },

    changeNum : function(e){
        alert("You selected option "+$(e.currentTarget).attr("id"))
    }
});

Rather than listening for click on your option elements, you should listen for change on your select element:与其监听option元素的点击,不如监听select元素的变化:

events: {
    "change #select":"changeNum"
}

Then in your changeNum function you can get the selected option:然后在您的changeNum函数中,您可以获得选定的选项:

changeNum: function(e) {
    alert("You selected option "+$(e.currentTarget).find('option:selected').attr('id'));
}

Here is a working fiddle: https://jsfiddle.net/vt7t47br/这是一个工作小提琴: https : //jsfiddle.net/vt7t47br/

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

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