如何使用jquery获取第n个子值

时间:2016-03-17 10:27:39

标签: javascript jquery css

我有以下html:

 <div id="buildyourform">

    <div class="fieldwrapper" id="field1">
        <input type="text" placeholder="Co-Author Name" class="fieldname m-wrap medium">
        <input type="text" placeholder="Co-Author Affiliation/Organization" class="fieldtype m-wrap medium">
    </div>
    <div class="fieldwrapper" id="field2">
        <input type="text" placeholder="Co-Author Name" class="fieldname m-wrap medium">
        <input type="text" placeholder="Co-Author Affiliation/Organization" class="fieldtype m-wrap medium">
    </div>

</div>

我想从buildyourform的每个div中获取输入框的值

<script>
    $('#buildyourform > div:nth-child(i) > input.fieldname').val()
</script>

我尝试了上面的代码,但它没有工作请求帮助。

5 个答案:

答案 0 :(得分:2)

您可以像以下一样使用if (fcol.getAnnotation(Transient.class) != null)) { // do something }

:eq()

答案 1 :(得分:1)

取自JQuery Docs:

jQuery( ":nth-child(index/even/odd/equation)" )

:nth-child选择器将被解析。如果您使用索引(整数),那么它将返回所有作为其父级的第n个子集的元素。因此,根据设置,它将返回0-n元素

jQuery( ":eq(index)" )

:eq选择器需要索引(整数)并返回给定索引处的元素(在集合中)和仅返回此

:nth-child :eq之间的巨大差异:

:nth-child选择器将返回作为其父节点的第n个子节点的所有元素(集合),而:eq将仅返回集合中给定索引处的1个元素! / p>

答案 2 :(得分:1)

您也可以使用此

boolean banana = true; (banana == true || false) ? System.out.println("True") : System.out.println("False");

答案 3 :(得分:0)

您的脚本只获取第一个输入的值 如果要获取每个输入的值,可以使用$ .each()函数。

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    void inject(MyApplication application);
    Context getApplicationContext();
}

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

@ActivityScope
@Component(dependencies={ApplicationComponent.class}, modules={ActivityModule.class})
public interface ActivityComponent extends ApplicationComponent {
}

答案 4 :(得分:0)

起初我正在使用jquery的第n个子属性尝试我的解决方案但是我使用eq属性得到了我的答案:

$('#buildyourform > div:eq(' + (i-1) + ') > input.fieldname').val()
$('#buildyourform > div:eq(' + (i-1) + ') > input.fieldtype').val()

此代码将在for循环中运行。其中“i”是for循环的值。