Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找具有特定类的最后一个div的子级计数_Javascript_Jquery_Html - Fatal编程技术网

Javascript 查找具有特定类的最后一个div的子级计数

Javascript 查找具有特定类的最后一个div的子级计数,javascript,jquery,html,Javascript,Jquery,Html,我的Html代码 <div class="col-md-6" id="buyerblock"> <div class="form-inline buyerdiv"> <div class="form-group"> <input class="form-control" name="buyername1" id="buyername1" style="margin-bottom: 3px;margin-right : 5px" v

我的Html代码

<div class="col-md-6" id="buyerblock">
 <div class="form-inline buyerdiv">
   <div class="form-group">
        <input class="form-control" name="buyername1" id="buyername1" style="margin-bottom: 3px;margin-right : 5px" value="" placeholder="Enter Buyer Name" type="text">
        <input class="form-control" name="buyeremail1" id="buyeremail1" style="margin-bottom: 3px" value="" placeholder="Enter Buyer Email" type="email">
        <button type="button" id="removebuyerbtn" class="btn btn-danger removebutton" style="margin-left: 19px;">-</button>
    </div>
  </div>
 <div class="form-inline buyerdiv">
  <div class="form-group">
        <input class="form-control" name="buyername2" id="buyername2" style="margin-bottom: 3px;margin-right : 9px" value="" placeholder="Enter Buyer Name" type="text"><input class="form-control" name="buyeremail2" id="buyeremail2" style="margin-bottom: 3px" value="" placeholder="Enter Buyer Email" type="email">
        <button type="button" id="removebuyerbtn" class="btn btn-danger removebutton" style="margin-left: 19px;">-</button></div></div>
 <div class="form-inline buyerdiv">
  <div class="form-group">
        <input class="form-control" name="buyername3" id="buyername3" style="margin-bottom: 3px;margin-right : 9px" value="" placeholder="Enter Buyer Name" type="text">
        <input class="form-control" name="buyeremail3" id="buyeremail3" style="margin-bottom: 3px" value="" placeholder="Enter Buyer Email" type="email">
        <button type="button" id="addbuyerbtn" class="btn btn-primary addbutton" style="margin-left: 19px;">+</button>
        <button type="button" id="removebuyerbtn" class="btn btn-danger removebutton" style="margin-left: 19px;">-</button>
  </div>
 </div>
</div>
我想得到最后一个buyerdiv类下FormGroup类的子元素计数。我怎样才能做到呢? 非常感谢您的帮助……

使用


使用
find()

var check=$(“div.buyerdiv”).last().find(“.form group”).children().length;
控制台日志(检查)

-
-
+
-

下面是一个关于fiddle with
find()
的示例

var count=$('#buyerblock').find('.form group').size();
警报(计数)

-
-
+
-

请尝试下面的代码,只需使用下面的行即可

$("div.buyerdiv").last().find(".form-group").children().length;
var check=$(“div.buyerdiv”).last().find(“.form group”).children().length;
警惕(检查)

-
-
+
-

您犯了一个小错误

只需使用find()而不是$

var check=$(.div.buyerdiv”).last().find(“.form group”).children().length; 警惕(检查)

我会使用
.children('.form group')
而不是
find
,尤其是在可能有大量子体或
表单组
元素可以嵌套的情况下。它将只查看最后一个
buyerdiv
元素的子元素,而不是所有降序元素。
var check = $("div.buyerdiv").last().find(".form-group").children().length;
    alert(check);
$("div.buyerdiv").last().find(".form-group").children().length;