javascript 使用 jquery 获取 <td> 中隐藏字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26419058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:56:46  来源:igfitidea点击:

Getting the value of a hidden field in <td> using jquery

javascriptjqueryhtmlcssasp.net-mvc-4

提问by Debashis Paul

I have a table data which is generated dynamically via a loop. The td contains a hidden field. below is the code for the same:

我有一个通过循环动态生成的表数据。td 包含一个隐藏字段。下面是相同的代码:

<td class="gridtd" id = "r<%=RowNumber%>c<%=ColumnNumber%>">
<input id="hiddendata" type="hidden" value="<%: item.Key%>"/>
</td>

I need to extract the value of the hidden field based on the td selected using jQuery. Please help me get the correct jquery code.

我需要根据使用 jQuery 选择的 td 提取隐藏字段的值。请帮我获取正确的 jquery 代码。

回答by Ionic? Biz?u

Just select your input and take the value (val()):

只需选择您的输入并取值 ( val()):

$("#hiddendata").val();

If you want to take all hidden input values:

如果要获取所有隐藏的输入值:

$("input[type='hidden']").each(function () {
   console.log($(this).val());
});

Note that the element ids must be unique.

请注意,元素 id 必须是唯一的

I need to extract the value of the hidden field based on the td selected using jQuery.

我需要根据使用 jQuery 选择的 td 提取隐藏字段的值。

If by selectyou mean, click, you can simply pass thiswhen getting the value:

如果通过选择你的意思,点击,你可以this在获取值时简单地传递:

$("td").on("click", function () {
   console.log(
     $("[type='hidden']", this).val()
   );
});

For your general knowledge, if you do $("#hiddendata", this).val();inside of the click handler, it will return the correct value (even having multiple ids with the same value).

就您的常识而言,如果您$("#hiddendata", this).val();在点击处理程序内部执行操作,它将返回正确的值(即使有多个具有相同值的 id)。

But definitely, the ids must be unique.

但肯定的是,ID 必须是唯一的。

回答by Janak

Use this :

用这个 :

  $('#hiddendata').val();

回答by Gibbs

$('td').click(
    function(event)
    {
      $(event.target).find('#hiddendata').val();
    }
);

It ll give the hiddendata value based on td selection

它将根据 td 选择给出 hiddendata 值

回答by Ganesh Jadhav

This will give the value of the hidden field for the selected td.

这将给出所选 的隐藏字段的值td

$('.gridtd').click(function(){
    console.log($(this).find('input').val());
});

回答by Harshit Jain

$('.gridtd').click(function(){
    console.log($(this).find('input[type=hidden]').val());
});

回答by PoojaMukne

You can try this:

你可以试试这个:

$('.gridtd').each(function(){
    var currentId = $(this).attr('id');
    var hiddenval = $('#'+currentId).find('input[type=hidden]').val();
    alert(hiddenval);
})