使用jquery从id获取值

时间:2016-04-19 08:53:18

标签: jquery html html-table

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" >
    <thead> 
        <tr> 
            <th>Project Code</th> 
            <th>Project Name</th> 
            <th>Project REF ID</th> 
            <th>Effective Date</th> 
            <th>Close Date</th> 
            <th>Last Update Date</th>
            <th>Creation Date</th>
            <th>Created By</th>
            <th>Last Update By</th>
            <th>Action</th>
        </tr> 
    </thead> 
    <tbody> 

        <?php 
        //error_log(print_r($project,false));

        for($i=0;$i<count($project);$i++)
        { ?>
            <tr class="project_id"  value="<?php echo $project[$i]   ['project_id']; ?>">

            <td><?php echo $project[$i]['project_code']; ?></td>
                <td><?php echo $project[$i]['project_name']; ?></td>
                <td><?php echo $project[$i]['project_ref_id']; ?></td>
                <td><?php echo $project[$i]['effective_date']; ?></td>
                <td><?php echo $project[$i]['close_date']; ?></td>
                <td><?php echo $project[$i]['last_update_date']; ?></td>
                <td><?php echo $project[$i]['creation_date']; ?></td>
                <td><?php echo $project[$i]['created_by']; ?></td>
                <td><?php echo $project[$i]['last_update_by']; ?></td>
                <td><img class="edit" >src="http://spsvn01/RAIDLOG/application/assets/img/file_edit.png" alt="edit" >border=1 height=20 width=20> 

                    <img class="delete" >src="http://spsvn01/RAIDLOG/application/assets/img/notification_error.png" >alt="delete" border=1 height=20 width=20>
                </td>
             </tr>
        <?php } ?>




     </tbody>

</table>

<script type="text/javascript">
$(document).ready(function() {
   var table=  $('#example').dataTable(
                {
                "columnDefs": [
                    { "width": "10%", "targets": 0 }]
                });


    $('.delete').click(function () {
        var project_id = $('.project_id').val();
        alert(project_id);
  });           

} );
</script>

我想获得我的价值,我甚至不知道该怎么做......

4 个答案:

答案 0 :(得分:2)

$('.delete').click(function() {
var project_id = $(this).closest('tr').attr('.value');
   alert(project_id);
});
.attr('value'); not .attr('.value');

这项工作很好。

答案 1 :(得分:0)

您需要获取所点击元素的value祖先元素的tr属性值,以便

$('.delete').click(function() {
  var project_id = $(this).closest('tr').attr('.value');
  alert(project_id);
});

答案 2 :(得分:0)

您的HTML中存在一些语法问题,即某些结束标记错误和""的错误使用。好吧,如果我们谈谈你的问题,你为什么不简单地创建自己的Javascript函数并在任何你想要的地方调用它,随心所欲地传递它:

<script type="text/javascript">
function deleterow(project_id){
    alert(project_id);
}
</script>

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead> 
        <tr> 
            <th>Project Code</th> 
            <th>Project Name</th> 
            <th>Project REF ID</th> 
            <th>Effective Date</th> 
            <th>Close Date</th> 
            <th>Last Update Date</th>
            <th>Creation Date</th>
            <th>Created By</th>
            <th>Last Update By</th>
            <th>Action</th>
        </tr> 
    </thead> 
    <tbody> 

        <?php 
        //error_log(print_r($project,false));

        for($i=0;$i<count($project);$i++){?>
            <tr>
                <td><?php echo $project[$i]['project_code']; ?></td>
                <td><?php echo $project[$i]['project_name']; ?></td>
                <td><?php echo $project[$i]['project_ref_id']; ?></td>
                <td><?php echo $project[$i]['effective_date']; ?></td>
                <td><?php echo $project[$i]['close_date']; ?></td>
                <td><?php echo $project[$i]['last_update_date']; ?></td>
                <td><?php echo $project[$i]['creation_date']; ?></td>
                <td><?php echo $project[$i]['created_by']; ?></td>
                <td><?php echo $project[$i]['last_update_by']; ?></td>
                <td>
                    <img class="edit" src="http://spsvn01/RAIDLOG/application/assets/img/file_edit.png" alt="edit" border="1" height="20" width="20"/> 

                    <?php
                    // if you have string in 'project_id' use this IMG TAG.
                    ?>
                    <img class="delete" src="http://spsvn01/RAIDLOG/application/assets/img/notification_error.png" alt="delete" border="1" height="20" width="20" onclick="myfunction('<?php echo $project[$i]['project_id']; ?>');"/>

                    <?php
                    // if you have an integer in 'project_id' use this IMG TAG.
                    ?>
                    <img class="delete" src="http://spsvn01/RAIDLOG/application/assets/img/notification_error.png" alt="delete" border="1" height="20" width="20" onclick="deleterow(<?php echo $project[$i]['project_id']; ?>);"/>
                </td>
             </tr>
        <?php } ?>




     </tbody>

</table>

<script type="text/javascript">
$(document).ready(function() {
   $('#example').dataTable({
        "columnDefs": [{
            "width": "10%",
            "targets": 0
        }]
    });
} );

</script>
  

请在上面的代码中阅读我的评论。

答案 3 :(得分:0)

尝试这个

$('.delete').click(function() {
  var project_id = $(this).parents('tr').attr('value');
  alert(project_id);
});

$('.delete').click(function() {
  var project_id = $($(this).parents('tr')).attr('value');
  alert(project_id);
});