简体   繁体   English

在jQuery中使用循环添加元素不会出现在dom中

[英]adding element using loop in jquery doesn't appear in dom

I am adding elements using a json call. 我正在使用json调用添加元素。 The issue I have is that if I append an item outside the loop it appears in the DOM and I can select it, if I appended in the loop I cannot select it? 我的问题是,如果我在循环外附加一个项目,它会出现在DOM中并且可以选择它;如果我在循环中附加一个项目,我将无法选择它吗?

Any help would be appreciated. 任何帮助,将不胜感激。

     <ul data-role='listview' class='ui-listview' id="DivLogOut" >
     </ul>



<script type="text/javascript">

    $(document).ready(function () {

            $("#DivLogOut").append($("<li class='PersonLogout'>TEST ITEM</li>"));

            $.get("api/StaffLoggedIn.ashx?loid=" + loid, function (data) {

                var jsonobj = $.parseJSON(data);
                $(jsonobj).each(function (i, item) {
                    $("#DivLogOut").append($("<li class='PersonLogout'>TEST ITEM</li>"));
                })

            })

        $(".PersonLogout").click(function () {
            alert("test");
        })

    })
</script>

Dynamically appended element use with on() 使用on()动态附加元素

$(document).on("click",".PersonLogout",function () {
            alert("test");
        })

when added dynamically, you need to re-add the listener to your DOM element, this way Jquery knows which one to call. 当动态添加时,您需要将侦听器重新添加到DOM元素中,这样Jquery便知道要调用哪个监听器。 i suggest you add your .click method in a function that you can call after the async loop is done (your $.get ) EX: 我建议您将.click方法添加到异步循环完成后可以调用的函数中(您的$.get )EX:

 <ul data-role='listview' class='ui-listview' id="DivLogOut"> </ul> <script type="text/javascript"> $(document).ready(function() { $("#DivLogOut").append($("<li class='PersonLogout'>TEST ITEM</li>")); $.get("api/StaffLoggedIn.ashx?loid=" + loid, function(data) { var jsonobj = $.parseJSON(data); $(jsonobj).each(function(i, item) { $("#DivLogOut").append($("<li class='PersonLogout'>TEST ITEM</li>")); }) addListener(); }) function addListener() { $(".PersonLogout").click(function() { alert("test"); }) } addListener(); }) </script> 

Please use .on instead of .click method. 请使用代替。点击方法。对 .on method will work for dynamically added elements. .on方法适用于动态添加的元素。

$(document).on("click",".PersonLogout",function () {
  //You can access the item using $(this)
});

You're creating an element after the document has loaded. 文档加载后,您正在创建元素。 AFAIK elements that are dynamically created after the document has loaded has no event binded on it. 加载文档后动态创建的AFAIK元素没有绑定任何事件。

What you can do is to bind the event on document. 您可以做的是将事件绑定到文档上。 Remove the click event in the document ready function. 在文档准备功能中删除单击事件。

$(document).on('click', '.PersonLogout', function()
{
     alert('I have been clicked!');
});

You need to listen the event outside the reloaded view, apparently $("#DivLogOut"). 您需要在重新加载的视图之外监听事件,显然是$(“#DivLogOut”)。

$("#DivLogOut").on('click', '.PersonLogout', function(){
    ....
});

For sure, it works ;) 当然,它可以工作;)

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

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