<a href="#"><img src="images/jbl.png" id="idhims"/></a>
<a href="#"><img src="images/swastik.png" id="idhims2" name="img2name"/></a>当单击上面的任何一个图像时,我希望使用单击的图像的id从另一个div的mysql数据库中检索数据。
我想使用SELECT-Query中被点击图像的id从mysql数据库中检索数据。
发布于 2013-02-28 20:13:25
使用jQuery
将此内容包含在
<script src="http://code.jquery.com/jquery-latest.js"></script>和
$(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页面,它将返回被点击的图片的值给我们的用户
$return_arr = array();
$return_arr['frmdb'] = $_POST['imageid'];
echo json_encode($return_arr);发布于 2013-02-28 20:02:16
添加函数onclick="yourFunction(this.id)",示例:
<script>
function yourFunction(id) {
alert(id);
}
</script>
发布于 2013-02-28 20:15:35
对于您的查询,我使jsfiddle如下所示
HTML
<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
$("img").click(function() {
alert(this.id);
});for jsfiddle example click here
https://stackoverflow.com/questions/15134809
复制相似问题