[英]How to get dynamic hidden id value in jquery?
In html, I have used hidden fields with dynamic ids. 在html中,我使用了带有动态ID的隐藏字段。
<a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="123" />something 1</a>
<a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="456" />something 2</a>
<a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="789" />something 3</a>
Here I am getting the id of the above hidden element in jquery. 在这里,我正在jquery中获取上述隐藏元素的ID。
var hiddenID = $('input[name$="edit_hid"]').attr('id');
alert(hiddenID );
Whenever I click the hyperlink something 1, something 2, something 3, I am always getting the id as edit_0
每当我单击超链接时,东西1,东西2,东西3,我总是得到id为
edit_0
How can I get the dynamic id for each hyperlink? 如何获得每个超链接的动态ID? so that I can get the value of those dynamic ids.
这样我就可以获得这些动态ID的值。
One solution is to use :hidden
in jquery selector: 一种解决方案是在jquery选择器中使用
:hidden
:
$("a").on("click", function(){ alert($(":hidden", this).val()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="123" />something 1</a> <a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="456" />something 2</a> <a><input type="hidden" name="edit_hid" id="edit_'+id+'" value="789" />something 3</a>
Some thing like this might work, could you be more clear, how were these link dynamically aadded, this may effect how this should work? 像这样的事情可能会起作用,您是否更清楚,这些链接是如何动态添加的,这可能会影响它应该如何工作?
<script>
$(function(){
$(".hidden").click(function(){
var a = $(this).attr("id");
alert(a);
});
});
</script>
you are using $('input[name$="edit_hid"]')
that returns a collection (array) 您正在使用
$('input[name$="edit_hid"]')
返回集合(数组)
when you take attr('id')
it will return the first [0]
(id=0) 当您使用
attr('id')
,它将返回第一个[0]
(id = 0)
so you should use 所以你应该使用
var a=$('input[name$="edit_hid"]')[0].attr('id');
var b=$('input[name$="edit_hid"]')[1].attr('id');
var c=$('input[name$="edit_hid"]')[2].attr('id');
check for length property of the collection $('input[name$="edit_hid"]').length
(it should be 3) 检查集合
$('input[name$="edit_hid"]').length
length属性(应为3)
I think you don't need click function over a hidden input. 我认为您不需要隐藏输入的点击功能。
Try this. 尝试这个。
$(document).ready(function() {
var hiddenID;
$('input').live("focus", function() {
hiddenID = $(this).attr('id');
alert(hiddenID );
});
});
`focus will binds only to elements that exist at the time it's called. `focus将仅绑定到调用时存在的元素。
live()
binds a function to an event for all existing elements, and any that are added to the DOM later. live()
将一个函数绑定到所有现有元素以及以后添加到DOM中的任何元素的事件。
Hope it will help you :) 希望它能对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.