[英]In a JQuery dblclick() listener of <tr>, how to get contents of all child <td> elements, as well as the contents of all <td>'s in table header row?
$("table tr.table-row").dblclick(function() { alert("Double click event received."); alert($(this)[0].outerHTML); console.log($(this)[0].outerHTML); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="responsive-table"> <thead> <tr> <td>Name</td> <td>Place</td> <td>Animal</td> <td>Thing</td> </tr> </thead> <tbody> <!-- A Number Of Rows --> <tr class="table-row"> <td>Bob</td> <td>Berlin</td> <td>Mouse</td> <td>TV</td> </tr> <!-- A Number Of Rows --> </tbody> </table>
I am writing a JQuery double-click listener for a table row, ie HTML tr
element. 我正在为表行(即HTML
tr
元素)编写JQuery双击侦听器。
In the HTML table, the tr
in the thead
element consists of column-names, ie name of fields whose values will be given in each tr
in the tbody
. 在HTML表中,
thead
元素中的tr
由列名组成,即列名,其值将在tbody
中的每个tr
中给出。
Ideally, in my JQuery dblclick()
listener, I need the names/keys as well as values for all the fields in the row which is double-clicked. 理想情况下,在我的JQuery
dblclick()
侦听器中,我需要双击行中所有字段的名称/键以及值。 Ideally, like so: 理想的情况如下:
Name : Bob
Place : Berlin
Animal : Mouse
Thing : TV
The reason for needing these kinda name-value pairs is that, from my JQuery double click listener, I need to send this data via AJAX
to a PHP script, which does some further server-side processing on it. 需要这些种类的名称-值对的原因是,从我的JQuery双击侦听器中,我需要通过
AJAX
将此数据发送到PHP脚本,该脚本对该文件进行进一步的服务器端处理。
For now, all I have been able to get is the outer HTML of the double-clicked element. 现在,我所能获得的只是双击元素的外部HTML 。 From the output of
console.log($(this)[0].outerHTML);
从
console.log($(this)[0].outerHTML);
: :
<tr class="table-row">
<td>Bob</td>
<td>Berlin</td>
<td>Mouse</td>
<td>TV</td>
</tr>
The question is how do I get the value of each in the clicked, preferably along with key/name which are in the 's in the table-header-row. 问题是如何获取所单击的每个值,最好是与表头行中的的键/名称一起。
How would you do this? 你会怎么做? Any tips/suggestions/advice is welcome.
欢迎任何提示/建议/建议。
I think this is what you're looking for. 我认为这就是您要寻找的。 Output is a
key => value object
. 输出是
key => value object
。
I grab all information first, do some trimming and removing empty values . 我首先获取所有信息,进行一些修整并删除空值 。 Those clean
Arrays
are processed_keys
and processed_values
. 那些干净的
Arrays
是processed_keys
和processed_values
。
In the last step I assign those arrays
as key => value
pairs to my data
object. 在最后一步中,我将这些
arrays
作为key => value
对分配给我的data
对象。
EDIT: This is not the shortest solution of course, you could combine the loops for example. 编辑:这当然不是最短的解决方案,例如,您可以组合循环。 But for demonstration purpose this should be enough =).
但是出于演示目的,这应该足够=)。
Here is the working fiddle: 这是工作的小提琴:
$("table tr.table-row").dblclick(function(e) { var values = $(e.target).parent().text().split('\\n'); var keys = $('thead').text().split('\\n'); var processed_keys = new Array(); var processed_values = new Array(); var data = {}; for(var i = 0; i < keys.length; i++){ if(keys[i].trim() !== ""){ processed_keys.push(keys[i].trim()); } } for(var i = 0; i < values.length; i++){ if(values[i].trim() !== ""){ processed_values.push(values[i].trim()); } } for(var i = 0; i < processed_keys.length; i++){ data[processed_keys[i]] = processed_values[i]; } console.log(data); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" rel="stylesheet"/> <table class="responsive-table"> <thead> <tr> <td>Name</td> <td>Place</td> <td>Animal</td> <td>Thing</td> </tr> </thead> <tbody> <!-- A Number Of Rows --> <tr class="table-row"> <td>Bob</td> <td>Berlin</td> <td>Mouse</td> <td>TV</td> </tr> <tr class="table-row"> <td>Luke</td> <td>Munich</td> <td>Giraffe</td> <td>Radio</td> </tr> <!-- A Number Of Rows --> </tbody> </table>
UPDATED
更新
target
is what you need instead of this
. target
是您需要的而不是this
。
$("table tr.table-row").dblclick(function(e) { var pos = $(e.target).index() var name = $(e.target).closest("table").find("tr >td").eq(pos).text() console.log(name+": "+$(e.target).text()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="responsive-table"> <thead> <tr> <td>Name</td> <td>Place</td> <td>Animal</td> <td>Thing</td> </tr> </thead> <tbody> <!-- A Number Of Rows --> <tr class="table-row"> <td>Bob</td> <td>Berlin</td> <td>Mouse</td> <td>TV</td> </tr> <!-- A Number Of Rows --> </tbody> </table>
Note: materialize.min.css gives an error but we dont need it to make this jquery example. 注意:materialize.min.css给出了一个错误,但我们不需要它来制作此jquery示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.