防止触发 Backbone.js 事件

abithluo  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(221)

我有一个带有 Backbone.js 视图的基本表单,当我在顶部输入上按Enter时,open事件被触发。我如何确保open事件只在我按浏览按钮时触发?

<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

chhkpiq4

chhkpiq41#

您需要指定type="button",否则它默认为submit,并且在表单中的文本框输入上按Enter键会触发submit...
http://jsfiddle.net/PJhED/40/

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

qlvxas9a2#

将其更改为<button class="browse" type="button">Browse...</button>
http://jsfiddle.net/2Rt6p/1/

相关问题