douzai3399
douzai3399
2012-11-03 15:17

javascript里面的php错误ReferenceError:img没有定义

已采纳

photos[] is an array of photos names then,

foreach($photos as $key=>$value)
{
    @$blocks .= '<div><img src="'.$value.'" onload="img=this"><img src="remove.png" onclick=remove_image(img)></div>';
}

so when clicking on the remove icon for the first photo it works just fine, but the others shows this error:

ReferenceError: img is not defined

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dsa5233 dsa5233 9年前

    Thank you LukeGT, your answer is right, but I found it better to use jquery to handle it,

    onclick="remove_image($(this).prev())"
    

    or

    onclick="remove_image($(this).parent())"
    

    to remove the whole div.

    点赞 评论 复制链接分享
  • duanquyong8164 duanquyong8164 9年前

    You should refresh page when clicked delete button.

    点赞 评论 复制链接分享
  • douying7289 douying7289 9年前

    The img variable is being stored globally, which means that it's getting overwritten each time a new image loads. You should instead assign the images unique ID's, and have their remove buttons remove the image by ID. For example:

    foreach($photos as $key=>$value)
    {
        @$blocks .= '<div><img src="'.$value.'" id="img'.$key.'"><img src="remove.png" onclick=remove_image(document.getElementById("img'.$key.'"))></div>';
    }
    

    (I'm not super familiar with PHP, so sorry if I made a syntax error somewhere)

    点赞 评论 复制链接分享