简体   繁体   English

jQuery-从按钮单击事件中获取具有包含特定值的字段的行的表头值

[英]Jquery - get table header value from a row that has a field with a specific value, from a button click event

I am using a horizontal table with a select list (dropdown) as one of the fields. 我正在使用带有选择列表(下拉列表)的水平表作为字段之一。 When I click submit I need to grab the table header value for that column and the value from the dropdown. 当我单击提交时,我需要获取该列的表头值和下拉列表中的值。

My table class = "views-table" essentially looks like this: 我的表class = "views-table"本质上是这样的:

header1 | header2 | header3 | header 4
--------------------------------------
select    select    select   select
data1     data2     data3    data4 

[submit button]

My dropdown class="request-select" element holds the following values: 我的下拉菜单class="request-select"元素包含以下值:

select list:
  -empty
  -FIRST
  -SECOND

So after a user selects a first value the table should look like this: 因此,在用户选择第一个值之后,表格应如下所示:

header1 | header2 | header3 | header 4
--------------------------------------
select    FIRST    select   select
data1     data2     data3    data4 

[submit button]

And the data I am expecting after the user clicks submit is: header2 and FIRST 用户单击提交后,我期望的数据是: header2FIRST

I can grab the table header value from the change event from the dropdown with: 我可以从下拉菜单中的change事件中获取表头值:

jQuery('table.views-table').on('change', 'td', function(e) {  
    var tableHead= e.delegateTarget.tHead.rows[0].cells[this.cellIndex];
    alert([jQuery(tableHead).text()]);  
    // returns `header2` from dropdown with `FIRST` value selected
});

But I can't figure out how to drill down and access this data from the submit button. 但是我不知道如何从“提交”按钮中进行追溯和访问此数据。 I don't want to use a hidden element or use localstorage. 我不想使用隐藏元素或使用localstorage。 Any ideas on how I should approach this? 关于如何处理此问题的任何想法? I am very inexperienced with jQuery. 我对jQuery非常缺乏经验。 If you need more information please let me know and I can provide more details if needed. 如果您需要更多信息,请告诉我,如有需要,我可以提供更多详细信息。

One of the approach would be providing related IDs to your headers and select tags. 一种方法是为标头和选择标签提供相关ID。 For example you can provide IDs like "select-1", "select-2"... to your dropdowns. 例如,您可以为下拉菜单提供ID,例如“ select-1”,“ select-2”...。 Similarly you have to provide IDs to your header tags like "thead-1", "thead-2"..., Now on submit click event, check which dropdown value has been changed(value!="") and then grab the last digit of that dropdown ID. 同样,您必须为标题标签提供ID,例如“ thead-1”,“ thead-2” ...,现在,在提交click事件时,检查哪个下拉值已更改(value!=“”),然后抓取该下拉ID的最后一位。 Now you can use that digit to get value of respected header. 现在,您可以使用该数字来获取受尊重的标头的值。

如果能够获取cells索引的句柄,则可以使用.val()而不是.text()来获取其值。

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

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