我像这样遍历复选框:
$('.day').each(function() {
// here I need to get the following 4 text inputs in the HTML
// and set some attributes on them
});
在每个复选框之后有四个文本输入字段(周围还有一些 div、CSS 的 span 标签)。所以在上面的循环中,我需要在 HTML 源代码中获取复选框后面的四个文本输入字段,以便我可以在它们上设置一些属性。
我该怎么做?
最佳答案
没有标记很难说,但你可以试试这个。
$('.day').each(function() {
$(this).nextAll('input').slice(0,4).attr('someAttribute','somevalue');
});
如果有一些停止点,比如另一个 .day
元素,你可以使用 nextUntil
然后.filter()
.
$('.day').each(function() {
$(this).nextUntil('.day').filter('input').attr('someAttribute','somevalue');
});
编辑:当你说有一些<div>
以及围绕它们的其他标记,我假设您的意思是在它们之间。
如果您实际上是说输入嵌套在其中,那么您可以这样做:
$('.day').each(function() {
$(this).nextAll(':has(input)').slice(0,4).find('input').attr('someAttribute','somevalue');
});
或者可能是这样的:
$('.day').each(function() {
$(this).nextAll().find('input').slice(0,4).attr('someAttribute','somevalue');
});
或者,如果有一个停止点,你可以像另一个一样指示 .day
, 使用 nextUntil()
:
$('.day').each(function() {
$(this).nextUntil('.day').find('input').attr('someAttribute','somevalue');
});
关于javascript - 如何在每个复选框后获得四个以下文本输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4681022/