Javascript 查找具有特定类的最后一个div的子级计数
我的Html代码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
<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 withfind()
的示例
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;