Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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_Twitter Bootstrap_Backbone.js - Fatal编程技术网

Javascript 为什么我的点击事件在主干视图内的模式按钮中多次附加

Javascript 为什么我的点击事件在主干视图内的模式按钮中多次附加,javascript,jquery,twitter-bootstrap,backbone.js,Javascript,Jquery,Twitter Bootstrap,Backbone.js,在后骨骼视图中,我正在渲染具有引导模式的模板。我的主干视图有一个引导模式窗口事件,当我第一次打开引导窗口时,它工作正常。下一次,如果我打开引导模式窗口,事件会多次附加 模板: <script id="adUser-template" type="template/underscore"> <div class="modal fade" id="adUserModal" tabindex="-1" role="dialog" aria-labelledby="myModa

在后骨骼视图中,我正在渲染具有引导模式的模板。我的主干视图有一个引导模式窗口事件,当我第一次打开引导窗口时,它工作正常。下一次,如果我打开引导模式窗口,事件会多次附加

模板:

<script id="adUser-template" type="template/underscore">
    <div class="modal fade" id="adUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Active Directory Search</h4>
                </div>
                <div class="modal-body">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" id='btnSearchUser' class="btn btn-primary">Search</button>
                </div>
            </div>
        </div>
    </div>
var ADSearchView = Backbone.View.extend({
        el: 'form',
        template: _.template($('#adUser-template').html()),
        initialize: function () {
            this.render();
        },
        render: function () {
            var renderedTemplate = this.template({});
            this.$el.append(renderedTemplate);
            $('#adUserModal').modal('show');
        },
        events: {
            'click #btnSearchUser': function (e) {
            e.preventDefault();
            this.adSearch();
            }
        },
        adSearch: function () {
            //below used to detach the click event once function called
                $('#adUserModal').on('hidden.bs.modal', function (event) {
                    $(this).find('#btnSearchUser').off('click');
                });
        },
    });

您可能正在向页面添加多个模态。。试试这个.el.html(renderedTemplate)而不是这个.el.append(renderedTemplate);谢谢,这帮我找到了根本原因