简体   繁体   English

jQuery Click事件未附加到Knockout模板

[英]jQuery Click event not attaching to Knockout template

I have a block of code that is attached to a jQuery click event. 我有一个附加到jQuery单击事件的代码块。 Here's the element: 这是元素:

<!-- Profiles -->
<div class="profiles">
    <h1>Profiles</h1>
    <div class="profile">
        <div class="name">
            <input type="text" maxlength="14" value="Default" />
            <span class="rename">q</span>
        </div>
        <div class="controls">
            <span class="edit">EDIT</span>
            <span class="duplicate">COPY</span>
            <span class="delete default">J</span>
            <div class="alert-box">
                <p>Are you sure you want to delete this profile?</p>
                <div>Y</div>
                <div>N</div>
            </div>
        </div>
        <div class="saved">
            <span class="cancel-button">Cancel</span><span class="save-button">Save</span>
        </div>
    </div>
</div>

When the item is selected, it becomes available for editing. 选择项目后,可以进行编辑。 Here's the event listener: 这是事件监听器:

        $('.rename').click(function () {
            $('.selected .rename').fadeIn(80);
            $(this).fadeOut(80);
            $(this).parent().addClass('selected');
        });

There's another event that listens for a click anywhere else on the page to deselect the item being edited: 还有另一个事件是在页面上的任何其他地方侦听单击以取消选择正在编辑的项目:

        $(document).click(function () {
            $(".selected .rename").fadeIn(80);
            $('.name').removeClass('selected');
        });

When it is clicked on, it should be selected to allow for editing. 单击时,应选择它以允许编辑。 When I move the code from the profile into a knockout template, it doesn't listen to the click event anymore. 当我将代码从profile移动到敲除模板时,它不再听取click事件。 When I inspect the Event Listeners in Chrome's tools, the listener is nowhere to be found. 当我检查Chrome工具中的事件监听器时,无法找到监听器。 Here is what my template looks like: 这是我的模板的样子:

            <div class="profiles">
                <h1>Profiles</h1>

                <div data-bind="template: { name: 'profilestempl', foreach: $root.profiles }"></div>
            </div>

        <script type="text/html" id="profilestempl">
            <div class="profile">
                <div class="name">
                    <input type="text" maxlength="14" data-bind="value: name" />
                    <span class="rename">q</span>
                </div>
                <div class="controls">
                    <span class="edit">EDIT</span>
                    <span class="duplicate">COPY</span>
                    <span class="delete">J</span>
                    <div class="alert-box">
                        <p>Are you sure you want to delete this profile?</p>
                        <div>N</div><div>Y</div>
                    </div>
                </div>
                <div class="saved">
                    <span class="cancel-button">Cancel</span><span class="save-button">Save</span>
                </div>
            </div>
        </script>

Can someone explain to me why the event listener no longer works on the dynamically added elements? 有人可以向我解释为什么事件监听器不再适用于动态添加的元素吗? I would also like help in solving this problem. 我也想帮助解决这个问题。 Thanks! 谢谢!

You have to add click event listener on the outer element which is always visible (since it doesn't work on hidden elements). 您必须在外部元素上添加单击事件侦听器,该外部元素始终可见(因为它不适用于隐藏元素)。 And then add other selector for template code (which is hidden) 然后为模板代码添加其他选择器(隐藏)

Sample code would be: 示例代码为:

function addClickEventToCloseButton(){
    $("#outerAlwaysVisible").on('click','#templateHiddenInitially',function(){
        alert('works')
    });
}

如果您希望处理程序上,将在您应该使用以后被创建工作内容onhttp://api.jquery.com/on/

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

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