2014-09-23 73 views
0

我有一个ajaxCall函数删除图像,如果操作成功完成,将调用另一个函数,用于删除显示图像的div如何检索通过PHP传递的JavaScript对象?

在被传递到PHP变量,我尝试传递包含图像div对象,并在成功的功能我想检索该对象由JavaScript将其删除。但实际上我不知道这是可能的还是不可能的?!我有这样的错误:TypeError: $object.parent is not a function

这是我的代码:

HTML

<div> 
    <input type="hidden" name="imageID" id="imageID" value="1"> 
    <a href="#"> 
     <img src="a.jpg" alt=""> 
    </a> 
    <a class="del-btn-pic" href="#">Delete</a> 
</div> 

的JavaScript

$(document).on("click",".del-btn-pic", function() { 
     $this = $(this); 
     var imageID = $this.parent().children('input').val(); 
     var divObject = JSON.stringify($this); 
     ajaxCall('deleteImage', {object:divObject , imageID:imageID }, removeImageVisual) 
    }); 
function removeImageVisual(data) // -> This function after ajaxCall is called. the php code in success returns the div object which passed before, and in failure returns 0. 
{ 
    if(data != 0) 
    { 
     $divObject = JSON.parse(data); 
     $divObject.parent().remove(); 
    } 
    else 
    { 
     alert('Error!'); 
    } 
} 
+0

其实,在我的代码,我通过了'标签'的对象,并检索它(不'div'标签) – Highlan 2014-09-23 06:49:18

+0

它看起来很好untils的$ divObject.parent(),你是什么人。试图达到那个目标? $ divObject必须是像{'name':'Javier'}这样的json对象,并且它肯定没有父对象。 – Balder 2014-09-23 06:50:05

+0

@Balder正如我在我的评论中所说的,不幸的是,代码与我的问题有点不同!实际上devObject是'a'标签! – Highlan 2014-09-23 06:53:24

回答

0

不是传递你应该通过a标签选择器以便您可以按以下方式使用。

var selector = JSON.parse(data); 
$(selector).parent().remove(); 
0

你想要做的就是点击删除后删除DIV,但你没有将它发送到PHP和回JS,因为当你从PHP回是重复的,只是做这样的:

$(document).on("click",".del-btn-pic", function() { 
     $this = $(this); 
     var imageID = $this.parent().children('input').val(); 
     //var divObject = JSON.stringify($this); not needed 
     ajaxCall('deleteImage', {object:"" , imageID:imageID }, function(data) { 
      removeImageVisual(data,imageID); 
     }); 
    }); 


function removeImageVisual(data,imageID) 
{ 
    if(data != 0) 
    { 
     $('#' + imageID).parent().remove(); 
    } 
    else 
    { 
     alert('Error!'); 
    } 
}