【问题标题】:jQuery $(this) first child selection for inputjQuery $(this) 输入的第一个子选择
【发布时间】:2013-11-08 20:52:35
【问题描述】:

我一直使用tempClass 来遍历某些内容并选择第一个子文本输入。我现在对此有奇怪的问题。我一直在寻找正确的方法,但没有成功。

代码片段

$( ".inputQuantityUpdate" ).each(function( index ) {
    $(this).addClass("tempPtr");
    completed   = $(".tempPtr :input[type='text']:first").val();
    nc          = $(".tempPtr :input[type='text']:first").next().val();
    if(nc == "NC")nc = 0;
    $(this).removeClass("tempPtr");
    $(this).parent().remove();
});

我的问题是 nc 并且完成对于第一次迭代是正确的,但不是之后。 由于某种原因,这不起作用。解决这个问题的最佳方法是什么?

更新: 标记的解决方案有效。我应该发布我正在抓取的其他字段,因为它们不起作用。当迭代中只有一个元素“inputQuantityUpdate”时,这组例程有效。当有多个时它会爆炸。我有一种感觉,这是我的某种内存问题/语法滥用。如果元素中存储有数据,最好的方法是:

function updatesConfirmed(){
        $("#confirmInputValues").remove();
        $( ".inputQuantityUpdate" ).each(function( index ) {
            job         = $(this).data("job");
            suf         = $(this).data("suff");
            op          = $(this).data("op");
            completed   = $(":input[type='text']:first", this).val();
            nc          = $(":input[type='text']:first", this).next().val();
            if(nc == "NC")nc = 0;
            $(this).parent().remove();
        });
    }

【问题讨论】:

  • 好吧,你的每个循环基本上是毫无意义的。为什么你说?好吧,在您的第一行中,您为元素分配了一个 tempPtr 类,这一切都很好。下一行,您选择元素 .tempPtr 和第一个文本输入,每个循环,每次都将是相同的值
  • 您可能不想为此使用each 循环,因为您在每次迭代中都做同样的事情。

标签: jquery iteration each css-selectors


【解决方案1】:

我假设您想从当前元素中获取第一个文本输入,在这种情况下,您的选择器将更改为:

completed   = $(":input[type='text']:first", this).val();
nc          = $(":input[type='text']:first", this).next().val();

【讨论】:

  • 这条评论已被删除
  • 这条评论已被删除
【解决方案2】:

我假设您只使用tempPtr 来访问每个循环中的循环元素。如果是这种情况,您可以在 $(this) 上使用 .filter

试试这个:

$( ".inputQuantityUpdate" ).each(function( index ) {
    completed = $(this).filter(":input[type='text']:first").val();
    nc = $(this).filter(":input[type='text']:first").next().val();
    if (nc == "NC") nc = 0;
    $(this).parent().remove();
});

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2015-12-06
    • 2010-10-24
    • 1970-01-01
    • 2011-05-04
    • 2016-09-22
    • 2012-06-24
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多