我有几个表行和表,其中隐藏了输入字段和值。我需要做的是遍历所有隐藏的输入字段,找到一个特定的字段,其中class = ' something‘和value =something,然后转到该输入字段的<td>并更改背景颜色。
$('input:hidden').each(function(){
if( $(this).find(".id_schedule_hours") && $(this).val() == 1) {
console.log('here')
}
}发布于 2016-07-31 07:02:13
.each()不是必需的;您可以使用属性选择器.closest()
$("input:hidden[class='something'][value='something']")
.closest("td").css("backgroundColor", /* color */)发布于 2016-07-31 07:37:52
要添加要检查的唯一值-您可以使用数据属性,并在数据变量中设置要检查的所需值。然后在函数中-您获得数据属性值(与输入类无关),并比较该所需值的值:
//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');
}
});发布于 2016-07-31 06:52:38
使用类作为选择器,并使用.parent()在链中向上移动并更改父级:
$('input[type=hidden].id_schedule_hours').each(function(){
if( $(this).val() == 1) {
console.log('here');
$(this).parent().css('background-color','red');
}
});我还建议更改您的选择器名称-让一个名为.id_的类似乎是错误的……
同样-如果这个类的唯一输入是你感兴趣的隐藏输入,那么你可以直接在选择器中将它们作为目标,例如:
$('.id_schedule_hours').each(function(){https://stackoverflow.com/questions/38679178
复制相似问题