如何获取div中的所有表单属性?

Nik*_*wal 1 javascript jquery html5

我想要一个属性,如名称,表格的Id存在于div中

<div id="searchForms">

    <form id="form1" name="form1">    
    </form>

    <form id="form2" name="form2">    
    </form>
</div>

<form id="form3" name="form3">    
</form>
Run Code Online (Sandbox Code Playgroud)

那么我现在正在做什么

$('form').each(function () {

    console.log($(this).attr('id'));
    console.log($(this).attr('name'));

});
Run Code Online (Sandbox Code Playgroud)

但问题是,它给了我所有三种形式的名称和身份.我想要id中存在的div中所有表单的名称和id id="searchForms".

Adi*_*dil 6

你需要后代选择器("祖先后代"),使用父div的id为searchFormsas ancestor和form asdescendant

现场演示

$('#searchForms form').each(function () {    
    console.log($(this).attr('id'));
    console.log($(this).attr('name'));    
});
Run Code Online (Sandbox Code Playgroud)

描述:选择作为给定祖先后代的所有元素.