首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在点击图片时获取php变量中img标签的id?

如何在点击图片时获取php变量中img标签的id?
EN

Stack Overflow用户
提问于 2013-02-28 19:50:50
回答 4查看 3.6K关注 0票数 0
代码语言:javascript
复制
<a href="#"><img src="images/jbl.png" id="idhims"/></a>
<a href="#"><img src="images/swastik.png" id="idhims2" name="img2name"/></a>

当单击上面的任何一个图像时,我希望使用单击的图像的id从另一个divmysql数据库中检索数据。

我想使用SELECT-Query中被点击图像的id从mysql数据库中检索数据。

EN

回答 4

Stack Overflow用户

发布于 2013-02-28 20:13:25

使用jQuery

将此内容包含在

代码语言:javascript
复制
<script src="http://code.jquery.com/jquery-latest.js"></script>

代码语言:javascript
复制
$(document).ready(function(){
    $('a').click(function(){
        var imgId = $(this).children('img').attr('id');// store the id to varialbe imgId
        //alert(imgId);
        $.ajax({
            type: "POST",
            url: "process_form.php",
            data: {imageid:imgId},
            dataType: "json",
            success: function(data) {
                //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
                $("#message_ajax").html(data.frmdb);// message_ajax is the id of the container to show the returned value
            }
        });   
    });
})

您可以在php页面中以$_POST['imageid']的形式访问该值。

示例php页面,它将返回被点击的图片的值给我们的用户

代码语言:javascript
复制
$return_arr = array();
$return_arr['frmdb'] = $_POST['imageid'];
echo json_encode($return_arr);
票数 2
EN

Stack Overflow用户

发布于 2013-02-28 20:02:16

添加函数onclick="yourFunction(this.id)",示例:

代码语言:javascript
复制
    <script>
      function yourFunction(id) {
        alert(id);
      }
    </script>

票数 1
EN

Stack Overflow用户

发布于 2013-02-28 20:15:35

对于您的查询,我使jsfiddle如下所示

HTML

代码语言:javascript
复制
<img  src="http://google.com/img" id="id"/>
<img  src="http://google.com/img" id="id1"/>
<img  src="http://google.com/img" id="id2"/>

Js

代码语言:javascript
复制
$("img").click(function() {
  alert(this.id);
});

for jsfiddle example click here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15134809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档