简体   繁体   English

当我单击TR时如何获取隐藏的TD值

[英]How To Get Hidden TD Value when i click on a TR

im trying to make a task manger based on web system that can monitor tasks at work , im using html and php . 即时通讯试图建立一个基于Web系统的任务管理器,可以使用html和php监视工作中的任务。 im trying to show all the tasks in table and when i click on a task (row) i want to get a hidden td value (id of task) and send it as arg to another page for more information about the task . 我试图显示表中的所有任务,当我单击一个任务(行)时,我想获取一个隐藏的td值(任务的id),并将其作为arg发送到另一页以获取有关该任务的更多信息。 i searched the web how can i do that , but i didnt realy understand how to do it with my table . 我在网上搜索怎么做,但是我真的不知道该如何在桌子上做。 here is my table code 这是我的表代码

                   <table id="thickets_show">
                    <tr id="show_ticket_table_header">
                        <td>Work Week</td>
                        <td>Task Submited Date</td>
                        <td>Task Submited Time</td>
                        <td>Task</td>
                        <td>Project</td>
                        <td>Task Owner</td>
                        <td>Task Assigend to</td>
                        <td>Priority</td>
                        <td>Status</td>
                        <td>Task Total Work Time</td>
                        <td>Task Due Date</td>
                        <td>Task Finish Date</td>
                        <td>Team</td>
                    </tr>';
                    foreach ($tasks as $key => $value) {
                        if ($value['ticket_status']=="done")
                            echo '<tr id="done_ticket" >';
                        elseif ($value['ticket_priority']=="high")
                             echo '<tr id="high_ticket" class="ticket_raw">';
                        else
                            echo '<tr class="ticket_raw">';
                        echo '
                                <td>
                                    '.$value['work_week'].'
                                </td>
                                <td>
                                    '.$value['ticket_date'].'
                                </td>
                                <td>
                                    '.$value['ticket_time_submit'].'
                                </td>
                                <td>
                                    '.$value['ticket_request'].'
                                </td>
                                <td>
                                    '.$value['ticket_project'].'
                                </td>
                                <td>
                                    '.$value['ticket_onwer'].'
                                </td>
                                <td>
                                    '.$value['ticket_assigned_user'].'
                                </td>
                                <td>
                                    '.$value['ticket_priority'].'
                                </td>
                                <td>
                                    '.$value['ticket_status'].'
                                </td>
                                <td>
                                    '.$value['ticket_worktime'].'
                                </td>
                                <td>
                                    '.$value['ticket_due_date'].'
                                </td>
                                <td>
                                    '.$value['ticket_finish_date'].'
                                </td>
                                <td>
                                    '.$value['team'].'
                                </td>
                                <td style="display:none;" id="ticket_id class="divOne">
                                    '.$value['id'].'
                                </td>
                              </tr>

this is the function that im using 这是即时通讯使用的功能

$(function(){
            $(".ticket_raw" ).on(\'click\',function(e){
                e.preventDefault();
                var id = $(this).attr("#ticket_id");
                alert(id);
            });
        });

The td has the id ticket_id , so you can use document.getElementById() : td的ID为ticket_id ,因此您可以使用document.getElementById()

var td = document.getElementById('ticket_id');
var value = td.textContent;

Or, using jQuery: 或者,使用jQuery:

var td = $('#ticket_id');
var value = td.text();

There is a small issue to correct, then you can make this work nice and easily: 有一个小问题需要纠正,那么您可以轻松轻松地完成此工作:

echo '<tr id="done_ticket" >'; is invalid - if you're creating multiple elements in a loop like this, you can't give them all the same "id", that's illegal in the HTML specification. 是无效的-如果您要在这样的循环中创建多个元素,则不能给它们赋予相同的“ id”,这在HTML规范中是非法的。 You have to use classes instead. 您必须改为使用类。

Rewrite it slightly like this: 像这样重写它:

foreach ($tasks as $key => $value) {
  echo '<tr data-id="'.$value['id'].'" class="ticket ';
  if ($value['ticket_status']=="done")
    echo 'done_ticket';
  elseif ($value['ticket_priority']=="high")
     echo 'high_ticket ticket_raw';
  else
    echo 'ticket_raw';
  echo '">';
  echo '
    <td>
    //etc...

Note the extra class "ticket" on the tr element, regardless of the status, and the "data-id" attribute containing the ID (instead of a hidden field). 请注意tr元素上的额外类“ ticket”(与状态无关),以及包含ID(而不是隐藏字段)的“ data-id”属性。

Now, you can write some jQuery to get the ticket ID: 现在,您可以编写一些jQuery来获取票证ID:

$(".ticket").click(function(event) {
  var id = $(this).data("id"); //gets the data-id value from the clicked tr
  window.location.href = "ticket.php?id=" + id; //example URL to redirect to, passing the ID from the tr
});

Lastly you can delete this code: 最后,您可以删除以下代码:

<td style="display:none;" id="ticket_id class="divOne">
  '.$value['id'].'
</td>

 $('tr').on('click',function(){ var hidden_items = $(this).find('.td_hidden'); $.each(hidden_items,function(k,v){ console.log($(v).text()); }) }) 
 html,body{ height: 100%; margin: 0 auto; padding: 0px; } td{ border:1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <body> <table> <tr> <td>COLUMN1</td> <td>COLUMN2</td> <td>COLUMN3</td> <td>COLUMN4</td> </tr> <tr> <td>VALUE01</td> <td>VALUE02</td> <td>VALUE03</td> <td>VALUE04</td> <td style="display:none;" class="td_hidden">VALUE_HIDDEN_01</td> <td style="display:none;" class="td_hidden">VALUE_HIDDEN_02</td> </tr> <tr> <td>VALUE11</td> <td>VALUE12</td> <td>VALUE13</td> <td>VALUE14</td> <td style="display:none;" class="td_hidden">VALUE_HIDDEN_11</td> <td style="display:none;" class="td_hidden">VALUE_HIDDEN_12</td> </tr> </table> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM