$("[name=form_1]").each(function(){
alert("i in else"+i);
$('.eform_text').each(function() {
});
});
这个循环会遍历仅在 form1 中具有 eform_text 类的所有元素。还是会遍历具有该类的所有元素?
更新:
具体的jsp代码如下:
<c:when test="${eformDetails.controlType==1}">
<input id="textBox_${eformDetails.id}_${eformDetails.required}_${i}" class="eformDetail eform_text" type="text" value="" name="form_${i}" onblur="validateEformInputs(${i-1})"></input>
</c:when>
我的表格每次都不同。对于每种表格,我都需要获取所有文本框。目前在您的帮助下,我的 javascript 如下:
$("[name=form_"+i+"]").each(函数(i){ alert("我在别的地方"+i);
$('.eform_text', this).each(function() {
textboxId = $(this).attr("id");
它到达了第一个警报,但我无法到达第二个循环。它没有获取具有类 eform_text 的元素。不确定这里出了什么问题。你能帮忙吗?
最佳答案
它将遍历具有该类的所有 元素,无论是否在名为“form_1”的表单内。要仅查看每个表单(我猜你必须有多个名为“form_1”的表单,尽管这看起来很奇怪),请使用 find
在外循环中以限定内循环的范围:
$("[name=form_1]").each(function(formIndex) {
alert("formIndex in each: " + formIndex);
$(this).find('.eform_text').each(function(textIndex) {
alert("textIndex in each: " + textIndex);
});
});
或者您可以使用 $()
的第二个参数,它提供了工作环境:
$("[name=form_1]").each(function(formIndex) {
alert("formIndex in each: " + formIndex);
$('.eform_text', this).each(function(textIndex) {
alert("textIndex in each: " + textIndex);
});
});
两者都可以。
请注意,正如@Shrikant Sharat 在评论中指出的那样(感谢 Shrikant!),我假设您原始代码中的 i
是传递给 的索引每个
。我在上面显示了两个级别的索引(具有描述性名称)。
关于javascript - 我们可以在 jquery 的 .each 中使用 .each 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033613/