dqc22586 2016-07-27 21:37
浏览 28

如何将动态ID(php)传输到javascript?

I have created an image gallery with PHP. The user can delete each image: Clicking the button "Delete this Image?" will show him an image-overlay with 2 buttons "Delete" and "Cancel". The "Cancel"-button will hide the overlay.

Each image has it own ID. Now I want replace the "NAME-OF-IMG-WITHOUT-EXTENSION" in JavaScript with the ID created by PHP. Unfortunately, I do not know how to pass the ID to the Script... Here are the relevant code-areas:

<script>
$(document).ready(function(){
    $("#overlay_hide_" + "NAME-OF-IMG-WITHOUT-EXTENSION").click(function(){
        $(".overlay").fadeOut("fast");
    });
    $("#overlay_show_" + "NAME-OF-IMG-WITHOUT-EXTENSION").click(function(){
        $(".overlay").fadeIn("fast");
    });
});
</script>

<div class="overlay">
<a class="button_delete" onclick="deleteImage('<?php echo $filename['basename']; ?>')">Delete</a>
<a class="button_cancel" id="overlay_hide_<?php echo $filename['filename']; ?>">Cancel</a>
</div>

<img src="<?php echo $filename['basename']; ?>" />
<a id="overlay_show_<?php echo $filename['filename']; ?>">Delete this Image?</a>  
  • 写回答

1条回答 默认 最新

  • doutang1946 2016-07-27 21:43
    关注

    Embed the ID in your html somewhere. Then you can retrieve it via the click handler, e.g.

    <div class="img" data-id="<?php echo $img_id; ?>">
       <img ... />
       <button onclick="removeMe(event);">X</button>
    </div>
    
    function removeMe(e){ // e = click event
      button = e.target;
      div = button.parent;
      id = div.dataset.id;
      ... do stuff with the id
    }
    

    Alternatively, just embed the ID directly in the click handler call:

    <button onclick="removeMe(<?php echo json_encode($id); ?>);">X</button>
    
    function removeMe(id) {
       ... do stuff with id
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线