簡體   English   中英

獲取jQuery中的項目總數

[英]Getting the total number of items in jQuery

這是我的html:

<label id='custom'></label>
<p>test</p>
<label id='start_date[0]'></label>
<p>test</p>
<p>test</p>
<label id='start_date[1]'></label>
<label id='start_date[2]'></label>
<p>test</p>
<label id='custom2'></label>

我想獲取標簽總數,該標簽具有以下格式的ID: start_date[i] 我怎樣才能做到這一點? 你能給個主意嗎? 謝謝。

您可以使用“屬性始於”選擇器和length屬性:

 var itemCount = $('label[id^="start_date"]').length; alert(itemCount); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label id='custom'></label> <p>test</p> <label id='start_date[0]'></label> <p>test</p> <p>test</p> <label id='start_date[1]'></label> <label id='start_date[2]'></label> <p>test</p> <label id='custom2'></label> 

使用包含選擇器之類的屬性

$('[id*="start_date"]').length

如果希望它以start_date顯式開頭,請使用 ^而不是* 開頭屬性

$('[id^="start_date"]').length

您可以使用以選擇器[name ^ =“ value”開頭的屬性” ,然后獲取length 屬性

選擇具有指定屬性的元素,該元素的值恰好以給定字符串開頭。

 $('label[id^="start_date"]').length

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM