简体   繁体   中英

Prevent backbone event from firing

I have a basic form with a backbone view, when I press enter on the top input the open event is fired. How can I ensure that the open event only fires when I press the browse button?

<form method="post">
    <div>
        <input type="text" />
    </div>
    <div id="myId">
        <input type="text" />
        <button class="browse">Browse...</button>
    </div>
    <input type="submit" value="Save" />
</form>

var MyView = Backbone.View.extend({

    events: {
        'click button.browse': 'open'
    },

    open: function(e) {
        alert('Open dialog');
    },

    initialize: function() {},

    render: function() {}
});

$(function() {
    var myView= new MyView({ el: $('#myId') });
});​

​

jsFiddle here

You need to specify type="button" , otherwise it defaults to submit , and hitting enter on an textbox input in a form triggers submit...

http://jsfiddle.net/PJhED/40/

    <button class="browse" type="button">Browse...</button>

Change it to <button class="browse" type="button">Browse...</button>

http://jsfiddle.net/2Rt6p/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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