jquery最近的li不起作用

Wil*_*aan 0 jquery

我有一个链接的HTML页面

当用户点击该链接时,应该出现一个新的选择标记

HTML代码

<div class="container" id="addCell">
    <ul class="containerUL">
        <li class="containerLI">
            <p>
                <label>Name</label>
                <input type="text" class="longInput1"/>
            <p>
            <p>
                <label>City</label>
                <select id="countrySelector">
                </select>
            </p>
        </li>
        <li class="containerLI">
            <p>
                <label>Inserted cells</label>
                <a href="#" class="smallLink" id="acaclink">new</a>
            </p>
        </li>
        <li class="containerLI">
            <input type="submit" class="button1" value="save"/>
        </li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

jquery代码

$("#addCell").ready(function(){
    $("#addCell").on('click',"#acaclink",function(){
        $.getJSON("http://localhost/Mar7ba/Cell/getAllCells/TRUE",function(data){
            var options = '';
            options+="<option>select cell</option>";
            for(var i=0;i<data.length;i++){
                options += "<option>"+data[i]+"</option>";
            }
            $(this).closest('li').append('<p>\n\
            <label>Select Cell</label>\n\
            <select name="acSelect[]">\n\
             '+options+'\n\
</select>\n\
            </p>');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

1-我正在检查JSON呼叫并且没有错误2-我是警报选项,它可以正常工作

我的问题是:当我$(this).closes('li')$('ul').append 它替换它时,但是当我把它放在最近的时候它没有.请问哪里有错误

zer*_*kms 5

那是因为$(this)匿名函数内部有另一个范围并引用另一个对象

$("#addCell").ready(function(){
    $("#addCell").on('click',"#acaclink",function(){
        var me = this; // we use "me" as a closure to the object we clicked at

        $.getJSON("http://localhost/Mar7ba/Cell/getAllCells/TRUE",function(data){
            var options = '';
            options+="<option>select cell</option>";
            for(var i=0;i<data.length;i++){
                options += "<option>"+data[i]+"</option>";
            }
            $(me).closest('li').append('<p>\n\   // <---- here we use "me"
            <label>Select Cell</label>\n\
            <select name="acSelect[]">\n\
             '+options+'\n\
</select>\n\
            </p>');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)