2016-08-10 8 views
1

私の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> 

私のJSコード

var check = $("div.buyerdiv").last().$(".form-group").children().length; 
alert(check); 

私は、フォーム・グループの子要素の数を取得したいです最後のbuyerdivクラスの下のクラス。どうすればそれを達成できますか?すべてのヘルプははるかに高く評価され ...

答えて

1

使用find()

var check = $("div.buyerdiv").last().find(".form-group").children().length; 
    alert(check); 
+1

ありがとうございます。ばかげた間違い – Aamir

+0

私は 'find'の代わりに' .children( '。form-group') 'を使用したいと思います。特に、子孫の数が多い場合や' form-group'要素入れ子にすることができます。すべての降順要素ではなく、最後の 'buyerdiv'要素の子要素のみを検索します。 –

0

代わりに使用find()

var check = $("div.buyerdiv").last().find(".form-group").children().length; 
 
console.log(check);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<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>

0

$のは、ここでfind()とフィドルの一例です。

var count = $('#buyerblock').find('.form-group').size(); 
 
alert(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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>

0

ちょうどあなたが小さなミスを持って

$("div.buyerdiv").last().find(".form-group").children().length; 

var check = $("div.buyerdiv").last().find(".form-group").children().length; 
 
alert(check);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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>

0

次の行を使用して、次のコードを試してみてください。

だけのfind()の代わりに$

を使用するvarチェック= $( "div.buyerdiv")最後の()( "フォーム・グループを。")を見つける子供()の長さ。。。。。 アラート(チェック);