admin管理员组文章数量:1277886
I have a couple of table rows and tables with hidden input fields and values. What i need to do is loop through all the hidden input fields and find a specific field where the class = 'something' and value = something and go up to the <td>
of that input field and change the background color.
$('input:hidden').each(function(){
if( $(this).find(".id_schedule_hours") && $(this).val() == 1) {
console.log('here')
}
}
I have a couple of table rows and tables with hidden input fields and values. What i need to do is loop through all the hidden input fields and find a specific field where the class = 'something' and value = something and go up to the <td>
of that input field and change the background color.
$('input:hidden').each(function(){
if( $(this).find(".id_schedule_hours") && $(this).val() == 1) {
console.log('here')
}
}
Share
Improve this question
edited Jul 30, 2016 at 22:56
nnnnnn
150k30 gold badges209 silver badges247 bronze badges
asked Jul 30, 2016 at 22:47
YeakYeak
2,53810 gold badges46 silver badges73 bronze badges
4 Answers
Reset to default 6.each()
is not necessary; you can use attributes selector, .closest()
$("input:hidden[class='something'][value='something']")
.closest("td").css("backgroundColor", /* color */)
to add unique values to be checked against - you could use data attributes and set the desired value to check against in a data variable. Then in the function - you get the data attribute value (irrespective of the input class) annd pare the value of that desired value:
//HTML
<input type="hidden" class="id_schedule_hours" data-desiredvalue="1" name="id_schedule_hours" value="" />
<input type="hidden" class="id_trucks" data-desiredvalue="2" name="id_trucks" value="" />
//js
$('.id_schedule_hours,id_trucks').each(function(){
var desiredValue=$(this).attr('data-desiredvalue');
if( $(this).val() == desiredValue) {
console.log('here');
$(this).parent().css('background-color','red');
}
});
Use the class as a selector and use .parent() to go up the chain and change the parent:
$('input[type=hidden].id_schedule_hours').each(function(){
if( $(this).val() == 1) {
console.log('here');
$(this).parent().css('background-color','red');
}
});
Would also suggest changing your selector name - it seems wrong to have a class called .id_...
Also - if the only inputs that class are the hidden ones you are interested in, then you can just target them directly in the selector eg:
$('.id_schedule_hours').each(function(){
You could leverage the jQuery :hidden selector. It is not part of the official CSS selectors, but a jQuery selector.
$('input.id_schedule_hours:hidden').each(function(){
if( $(this).val() == 1) {
console.log('here')
}
}
Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation.
Elements that are not in a document are not considered to be visible; jQuery does not have a way to know if they will be visible when appended to a document since it depends on the applicable styles.
This selector is the opposite of the :visible selector. So, every element selected by :hidden isn't selected by :visible and vice versa.
During animations to show an element, the element is considered to be visible at the start of the animation.
How :hidden is determined was changed in jQuery 1.3.2. An element is assumed to be hidden if it or any of its parents consumes no space in the document. CSS visibility isn't taken into account (therefore $( elem ).css( "visibility", "hidden" ).is( ":hidden" ) == false). The release notes outline the changes in more detail.
jQuery 3 slightly modifies the meaning of :hidden (and therefore of :visible). Starting with this version, elements will be considered :hidden if they don't have any layout boxes. For example, br elements and inline elements with no content will not be selected by the :hidden selector.
See: jQuery :hidden documentation
本文标签: javascriptjquery find all hidden input fields with specific class nameStack Overflow
版权声明:本文标题:javascript - jquery find all hidden input fields with specific class name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741255009a2366500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论