dongyao2001 2015-05-03 10:51
浏览 44
已采纳

使用jquery动态获取property_id

I have a table that is dynamically generated. And in the table I have a button called view images. Every time this button is clicked, I need to get the image names from database for that particular property.

        <?php foreach($unapproved as $unapp):?>
        <td>
        <div id="#imagedisplay">
            <input id="property" type="text" value="<?php  echo $unapp->property_id; ?>" style="display :none">

        </div>

        <br/><b>Title : <?php  echo $unapp->title; ?></b> <br/><?php  echo nl2br($unapp->full_description); ?></td>
        <td>
            <a  id="view_image" class="btn btn-default" >View Images</a><br/><br/><br/>
            <a  class="btn btn-default" style="color:Green;">Approve</a><br/><br/><br/>
            <a  class="btn btn-default" style="color:red;">Delete</a>
        </td>
        <?php endforeach; ?>

Every time the view images is clicked, need to get the value (property_id) from <input id="property" type="text" value="<?php echo $unapp->property_id; ?>" style="display :none"> using jquery. so that i can do the ajax call.

My problem is, I want to get the property_id of the clicked. I tried this way but it works only for the first property.

  <script>
$('#view_image').click(function(e){
    e.preventDefault();
    var prop_id = $('#property').val();
    console.log(prop_id);
});
</script>

application looks like this

enter image description here

Simple words, I need the property_id of the clicked property.

Can anyone give me suggestions to get the id of the clicked property?

  • 写回答

1条回答 默认 最新

  • donglei2288 2015-05-03 11:01
    关注

    There are many ways to solve this issue, but one easy way is adding an data attribute to your link:

    <a class="view_image btn btn-default" data-property-id="<?php echo $unapp->property_id; ?>">View Images</a>
    

    and then changing your script to:

    $('.view_image').click(function(e){
        e.preventDefault();
        var prop_id = $(this).data('property-id');
        console.log(prop_id);
    });
    

    Just as bonusinfo: It is not allowed to have multiple elements with the same id. Use class' instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)