admin管理员组

文章数量:1391756

How do I add preventDefault() to the <button> in the event? What I have below is not working .. thanks

var View = Backbone.View.extend({

    initialize: function () {
        //console.log('initializing ' + this.options.blankOption);
        this.template = $('#list-template').children();
    },
    el: '#container',
    events: {
        'click button' : 'render'
    },
    render: function(){

        this.events.preventDefault(); // not working ????

        var data = this.model.get('data');

        $.each(data,function (i,v) {
            console.log(data.text + " " + data.href);
        });

    }
});

How do I add preventDefault() to the <button> in the event? What I have below is not working .. thanks

var View = Backbone.View.extend({

    initialize: function () {
        //console.log('initializing ' + this.options.blankOption);
        this.template = $('#list-template').children();
    },
    el: '#container',
    events: {
        'click button' : 'render'
    },
    render: function(){

        this.events.preventDefault(); // not working ????

        var data = this.model.get('data');

        $.each(data,function (i,v) {
            console.log(data.text + " " + data.href);
        });

    }
});
Share Improve this question asked Feb 25, 2013 at 9:11 Hello-WorldHello-World 9,55524 gold badges90 silver badges157 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

this.events is not a event object. so it not work

try this:

render: function(event){
    event && event.preventDefault();

    var data = this.model.get('data');

    $.each(data,function (i,v) {
        console.log(data.text + " " + data.href);
    });
}

Receive the event object as an argument

render: function(evt){

    evt.preventDefault();

    var data = this.model.get('data');

    $.each(data,function (i,v) {
        console.log(data.text + " " + data.href);
    });

}

You can re-activate the actions by adding

this.delegateEvents();  // Re-activates the events for all the buttons

If you add it to the render function of a backbone js view, then you can use event.preventDefault() as required.

本文标签: javascripthow do I add preventDefault() backbonejsStack Overflow