douhuijun3776 2015-07-29 09:57
浏览 42

如何使用jquery在行表上查找属性

if I have html that integrated on php like this :

foreach ($data_pc as $data) {
        ?>
        <tr>
            <td class="center"><?php echo $no++ . ". "; ?> </td>
            <td class="center" data-no="<?php echo $data['id_pc'] ?>"><?php echo $data['nama_user']; ?></td>
            <td class="center"><?php echo $data['perusahaan']; ?></td>



            <td  class="left">
                <a class="btn btn-info" name="edit" title="Edit User" req_id="<?php echo $data['nama_user']; ?>" data-toggle="modal" data-target="#myModal1" role="button">
                    <i class="halflings-icon white edit"></i> 
                </a>
            <td>
                <a class="btn btn-danger" id="btn-hapus" title="Hapus User" req_nama="<?php echo $data['nama']; ?>">
                    <i class="halflings-icon white trash" ></i>
                </a>
            </td>
            </td>
        </tr>
    <?php } ?>

that would be representated row on table html.

I want to take the attribute data-no on a row. So, I write jquery code like this :

$(document).on('click', '#btn-hapus', function() {
    var $row = $(this).closest("tr"); // Find row
    var data = $row.find("data-no").text(); // Find text
    alert(data);
});

It gives me a blank value ? How to find attribute on a row table using jquery ?

  • 写回答

1条回答 默认 最新

  • du8828 2015-07-29 09:58
    关注

    You need to use Has Attribute Selector [name] to target the element that have the required attribute:

    var data = $row.find("[data-no]").text(); // get text of element that have attr data-no
    

    if you want to get the attribute value of element then use .data() selector

    var data = $row.find("[data-no]").data('no'); //get data attribute value
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state