2012-05-15 98 views
0

删除图像在一个页面中,我显示图像的大拇指从一个文件夹,下面一个链接“删除图片”, 通过这个代码:用javascript函数

echo '<li> <a href="Gallery/'.$file.'" rel="lightbox['.$lightbox.']">'; 
echo '<div id="image" ><img src="Gallery/thumbs/'.$file.'" alt="" /><br> 
<a href="javascript:Del();"><font style="font-size:9px">Delete Image</font>'; 
echo '</a></div></a></li>'; 

在删除图片我调用一个JavaScript函数Del()。我想,如果用户点击删除图像时,图像拇指应删除,从它包含的文件夹,即廊/拇指。

功能德尔是:

<script type="text/javascript"> 
function Del(){ 
    var image_x = document.getElementById('image'); 
image_x.parentNode.removeChild(image_x); 
} 
</script> 

该函数从页面移除图像,而不是从文件夹中。而且它也会像删除它们一样,如果我在第三张图像上删除,它将首先删除,然后再删除它。 我想要的图像被删除,只有那些在其用户单击删除图片

我希望大家都明白。

+1

你需要使用不同的“ID”值,每一个元素。也就是说,多个元素具有id“image”是错误的。 – Pointy

+3

给每个图像的ID和使用Ajax的ID发送给PHP从文件夹中删除。 – Adam

+1

您无法直接从客户端删除图像。你需要从服务器上完成。 – Wilk

回答

0

您可以使用,因为它在评论AJAX从服务器中删除文件phisicaly说。 我劝你实现通用功能删除从服务器的元素,从数据库或文件 - 无论,原因一般你只需要调用一些网址, 与要删除,然后将对象传入ID - 变换DOM元素显示对象被成功删除的方式。

function remove(el, url, callback) { 
    $.post(url, {}, function(response) { 
     // Expect that the response would be in JSON format (PHP - json_encode()) 
     response = $.parseJSON(response); 

     // and in reponse JSON there should be the attibute called 'code', which is detects if the deleting where successfull, 
     // if it's == 0 that's success, if not - error 
     if (response.code == 0) { 
      el.remove(); 

      if (typeof callback == 'function') { 
       callback(response); 
      } 
     } 
    }); 
} 

这是您需要调用服务器端删除的功能。要使用它,把这个代码,你也应该在点击绑定事件触发的<a /> 这将启动删除这样:

$(document).on('click', 'a.delete', function(e) { 
    // Do not allow browser go to the link's href 
    e.preventDefault(); 

    // pass, as the first element div, which we need to delete from DOM, 
    // as the second argument - URL from <a /> to server-side script 
    // and third argument is th ecallback function which will remove() function call if the deleting where successfull 
    remove($(this).parents('div:first'), $(this).attr('href'), function(r) { 
     // here put the extra code to modify DOM after successfull delete 
    }); 
}); 

<a />的属性href你应该把链接到服务器端脚本,这会删除文件。

希望有所帮助。

+0

我想用Javascript来做到这一点。请给我一个新手,给我更多的细节 –