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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀