使用jQuery将数据与Ajax响应进行比较

时间:2018-08-01 06:34:04

标签: javascript jquery ajax

我的脚本如下

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
	   var id = $(this).attr('id');
	   $.ajax({
			contentType : "application/json; charset=utf-8",
			dataType : "json",
			url : "loadSubFolders",
			type : 'GET',
			data : {
				id : id
			},
			success : function(response) {
				$.each(response, function(index, value) {
						response.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
						alert("Appended Successfully");
				});
				$('.tree').append( response.join('') );
			},
			error : function(res, textStatus) {
				var msg = "Unable to load Subfolder";
				alert(msg);
			}
		});
        setStatus($(this));
    });
});

在这种情况下,我想将数据平均值id与某些响应元素进行比较

success : function(response) {
    		$.each(response, function(index, value) {
    	if(id==value.rootId){
    response.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
    	alert("Appended Successfully");
    	}
   else{
    response.push('<li class="tree-title" ></li>');
    alert("Append Failed");
    	}
   });
    $('.tree').append( response.join('') );
},

但是它不起作用

我该如何实现?有人可以建议我吗?

2 个答案:

答案 0 :(得分:1)

您可以为成功回调提供属性,并使用this从闭包内部访问它们。示例this.id您不能在ajax原型中定义您指定的自定义属性,否则,您将重写默认值。试试这个:

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
       var id = $(this).attr('id');
       $.ajax({
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            url : "loadSubFolders",
            type : 'GET',
            data : {
                id : id
            },
            id : id,
            success : function(response) {
                afterSuccess(response , this.id);

                function afterSuccess(data, i) {
                    $.each(data, function(index, value) {
                        if(i==value.rootId){
                            data.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
                            alert("Appended Successfully");
                        }
                        else{
                            alert("Append Failed");
                        }
                    });
                    $('.tree').append( data.join('') );
                }
            }
        });
        setStatus($(this));
    });
});

答案 1 :(得分:0)

您可以将结果加载到单独的变量中,例如contents

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
       var id = $(this).attr('id');
       $.ajax({
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            url : "loadSubFolders",
            type : 'GET',
            data : {
                id : id
            },
            id : id,
            success : function(response) {
                afterSuccess(response , this.id);
                function afterSuccess(data, i) {
                    var contents = "";
                    $.each(data, function(index, value) {
                        if(i==value.rootId){
                            contents += '<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>';
                            alert("Appended Successfully");
                        }
                        else{
                            alert("Append Failed");
                        }
                    });
                    $('.tree').append( contents );
                }
            }
        });
        setStatus($(this));
    });
});