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

时间:2015-07-29 09:57:05

标签: php jquery

如果我有像这样集成在php上的HTML:

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 } ?>

表示html上的行。

我想在一行中使用属性data-no。 所以,我写这样的jquery代码:

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

它给我一个空白值?如何使用jquery在行表上查找属性?

1 个答案:

答案 0 :(得分:1)

您需要使用 Has Attribute Selector [name] 来定位具有所需属性的元素:

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

如果要获取元素的属性值,请使用.data()选择器

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