search

Home  >  Q&A  >  body text

javascript - Through ajax, all the data is loaded in ul, and the ul li click event is added, but it cannot be triggered.

Through ajax, all the data is loaded in ul, and the ul li click event is added, but it cannot be triggered. If it is a ul event, it can be triggered.

ajax

 /*省市*/
$("#proNum").click(function(){        
        $.ajax({type:"post",
              url:"region/jsonRegion",
              dataType:"json",
              success:function(data){                  
            $("#gj_disProvince").toggleClass("active");
                var listr ="";
                for(var i = 0; i < data.length; i++){
                    listr +="<li class='com-li gj_dispro' proNum='"+data[i].id+"'>"+data[i].placename+"</li>";
                }
                $("#assessPro").html(listr); //添加全部城市            
           }
        
        });
    });

Implementation diagram

Label

                <p class="conselect mr-7" id="gj_disProvince">
                    <input class="inputype" readonly="readonly"placeholder="省份"
                        id="proNum" proNum="0">
                    <p class="p-r">
                        <p class="down-box">
                            <ul class="com-ul com-ul-288" id="assessPro">
                            </ul>
                        </p>
                    </p>
                </p>

event

    /*选择一个省 ,li[class='com-li gj_dispro'] 触发不了*/
    $("ul[class='com-ul com-ul-288']").click(function(){
         alert(1);    
        // 不明白为什么 ul 能触发,li就不行,
        //请教一下如何获取到选择中li的省市和id           
    });
学习ing学习ing2727 days ago1140

reply all(5)I'll reply

  • 黄舟

    黄舟2017-06-12 09:24:31

    $('selector').on('click','childSelector',function(){})
    使用 on() 方法添加的事件处理程序适用于当前及未来的元素

    reply
    0
  • 代言

    代言2017-06-12 09:24:31

    Because the ajax event is executed asynchronously, when you click, there is no li don structure

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-12 09:24:31

    Save the id in the attr of li when looping
    <li c_id="" >
    Click event to get
    var cid = $this.attr('c_id');

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-12 09:24:31

    Use $parentNode.on('click',childNode,callback) to bind events. The principle is the event bubbling mechanism

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-12 09:24:31

    Note three points:
    1. The event registration function should be registered after the Ajax call is successful. You can call the event registration function in the ajax callback function.
    2. Make sure your event registration function is not written incorrectly. Your event function here Wrong writing, directly assign a value to click, and then your class selector. I did not find any element whose class value is the value in your selector. If not, of course it cannot be selected.
    3. Make sure the event has been registered before you click on the event.

    reply
    0
  • Cancelreply