从DOM获取非唯一ID的jQuery [英] jquery to get non-unique IDs from the DOM
问题描述
; div id =column1>
...
< input checked =checkedtype =checkboxname =daysvalue =1id =id_days_0/>
< / div>
< div id =column2>
...
< input checked =checkedtype =checkboxname =daysvalue =1id =id_days_0/>
< / div>
我如何区分基于顶级div ID的两个输入?
显然,这不起作用:
$('#id_days_0')。attr ('checked')
有没有办法只专注于特定div中的一个输入的ID
如前所述,您应该始终使您的ID独一无二,替代课程将是一个很好的方法。 >
据说,您可以参考必要的项目,如下所示:
HTML:
< div id =column1>
首先< input checked =checkedtype =checkboxname =daysvalue =Chk1id =id_days_0/>
< / div>
< div id =column2>
Second< input checked =checkedtype =checkboxname =daysvalue =Chk2id =id_days_0/>
< / div>
JavaScript:
code> alert(Check1 =+ $(#column1#id_days_0)val()+\\\
+Check2 =+ $(#column2#id_days_0 );
这个可以可以工作的原因是由于引用了对象基于父对象,然后是子节点。
工作示例: http://jsfiddle.net/8VqtE/
say i have these two uniquely ID'd divs with input elements that are not uniquely ID'd inputs.
<div id="column1" >
...
<input checked="checked" type="checkbox" name="days" value="1" id="id_days_0" />
</div>
<div id="column2" >
...
<input checked="checked" type="checkbox" name="days" value="1" id="id_days_0" />
</div>
how can i differentiate between the two inputs based on the top level div ID?
Obviously, this does not work:
$('#id_days_0').attr('checked')
Is there a way to only focus on one input's ID from within a particular div?
As previously mentioned, you should always make your IDs unique and subsituting a class would be a good approach.
With that being said, you may reference the necessary items as follows:
HTML:
<div id="column1" >
First <input checked="checked" type="checkbox" name="days" value="Chk1" id="id_days_0" />
</div>
<div id="column2" >
Second <input checked="checked" type="checkbox" name="days" value="Chk2" id="id_days_0" />
</div>
JavaScript:
alert("Check1 = "+$("#column1 #id_days_0").val()+"\n"+"Check2 = "+$("#column2 #id_days_0").val());
The reason that this can work, is due to the referencing of the objects based off of referencing the parent first, and then the child.
Working example: http://jsfiddle.net/8VqtE/
这篇关于从DOM获取非唯一ID的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!