jquery不';行不通

jquery不';行不通,jquery,Jquery,我有一个带有链接的html页面 当用户单击该链接时,应显示一个新的选择标记 html代码 名称 城市 插入细胞 jquery代码 $(“#addCell”).ready(函数(){ $(“#addCell”)。在('单击',“#acacclink”,函数()上{ $.getJSON(“http://localhost/Mar7ba/Cell/getAllCells/TRUE,函数(数据){ var选项=“”; 选项+=“选择单元格”; 对于(var i=0;i,这

我有一个带有链接的html页面 当用户单击该链接时,应显示一个新的选择标记

html代码

  • 名称 城市

  • 插入细胞

jquery代码
$(“#addCell”).ready(函数(){
$(“#addCell”)。在('单击',“#acacclink”,函数()上{
$.getJSON(“http://localhost/Mar7ba/Cell/getAllCells/TRUE,函数(数据){
var选项=“”;
选项+=“选择单元格”;
对于(var i=0;i,这是因为匿名函数中的
$(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>');
        });
    });
});
$(“#addCell”).ready(函数(){
$(“#addCell”)。在('单击',“#acacclink”,函数()上{
var me=this;//我们使用“me”作为单击对象的闭包
$.getJSON(“http://localhost/Mar7ba/Cell/getAllCells/TRUE,函数(数据){
var选项=“”;
选项+=“选择单元格”;
对于(var i=0;i,这是因为匿名函数中的
$(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>');
        });
    });
});
$(“#addCell”).ready(函数(){
$(“#addCell”)。在('单击',“#acacclink”,函数()上{
var me=this;//我们使用“me”作为单击对象的闭包
$.getJSON(“http://localhost/Mar7ba/Cell/getAllCells/TRUE,函数(数据){
var选项=“”;
选项+=“选择单元格”;

对于(var i=0;i
,此
getJSON
回调中具有不同的含义

您可以在
getJSON
之外保留一个引用,然后在内部使用它

$("#addCell").ready(function(){
    $("#addCell").on('click',"#acaclink",function(){

        var that = this; // reference this

        $.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>";
            }

              // use it here
            $(that).closest('li').append('<p>\n\
            <label>Select Cell</label>\n\
            <select name="acSelect[]">\n\
             '+options+'\n\
</select>\n\
            </p>');
        });
    });
});

这相当于您的代码,因为
getJSON
只是一个包装器,但是您可以获得所有可用的选项,比如
context:
这在
getJSON
回调中具有不同的含义

您可以在
getJSON
之外保留一个引用,然后在内部使用它

$("#addCell").ready(function(){
    $("#addCell").on('click',"#acaclink",function(){

        var that = this; // reference this

        $.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>";
            }

              // use it here
            $(that).closest('li').append('<p>\n\
            <label>Select Cell</label>\n\
            <select name="acSelect[]">\n\
             '+options+'\n\
</select>\n\
            </p>');
        });
    });
});

这相当于你的代码,因为
getJSON
只是一个包装,但是你可以得到所有可用的选项,比如
context:

是的,非常感谢9分钟后我会接受你的答案是的,非常感谢9分钟后我会接受你的答案
    $.ajax({
        url:"http://localhost/Mar7ba/Cell/getAllCells/TRUE",
        context:this,
        dataType:'json',
        success: function(data){

          // your code
            $(this).closest('li').append('<p>\n\...'

        }
    });