admin管理员组文章数量:1332352
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 class="gridtd" id = "r<%=RowNumber%>c<%=ColumnNumber%>">
<input id="hiddendata" type="hidden" value="<%: item.Key%>"/>
</td>
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 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.
Share Improve this question edited Oct 17, 2014 at 6:32 Ionică Bizău 114k94 gold badges308 silver badges487 bronze badges asked Oct 17, 2014 at 6:24 Debashis PaulDebashis Paul 272 gold badges2 silver badges6 bronze badges 6-
3
$('#hiddendata').val();
– Regent Commented Oct 17, 2014 at 6:25 - But there are multiple hiddendata as the loop is creating multiple td. So how will jQuery know which is the correct hidden id corresponding to the td selected. – Debashis Paul Commented Oct 17, 2014 at 6:29
- 2 For adequate elements' selecting their IDs must be unique. It is a bad style to use multiple same IDs. But in this situation you can use Ionică Bizău, P5Coder and user3168736 answers. – Regent Commented Oct 17, 2014 at 6:30
- what if my hidden field id is also a variable to the effect of id=hdnr<%=RowNumber%>c<%=ColumnNumber%> How will I then be able to get the value of the hidden field? – Debashis Paul Commented Oct 17, 2014 at 6:56
-
Then you can either select element by its ID or use, for example, mentioned
$('.gridtd').click(function() { console.log($(this).find('input[type=hidden]').val()); });
– Regent Commented Oct 17, 2014 at 7:00
6 Answers
Reset to default 2Just select your input and take the value (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.
I need to extract the value of the hidden field based on the td selected using jQuery.
If by select you mean, click, you can simply pass this
when getting the value:
$("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).
But definitely, the ids must be unique.
Use this :
$('#hiddendata').val();
$('td').click(
function(event)
{
$(event.target).find('#hiddendata').val();
}
);
It ll give the hiddendata value based on td selection
This will give the value of the hidden field for the selected td
.
$('.gridtd').click(function(){
console.log($(this).find('input').val());
});
$('.gridtd').click(function(){
console.log($(this).find('input[type=hidden]').val());
});
You can try this:
$('.gridtd').each(function(){
var currentId = $(this).attr('id');
var hiddenval = $('#'+currentId).find('input[type=hidden]').val();
alert(hiddenval);
})
本文标签: javascriptGetting the value of a hidden field in lttdgt using jqueryStack Overflow
版权声明:本文标题:javascript - Getting the value of a hidden field in <td> using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322152a2453004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论