我正在尝试收集几个select
的长度。如何知道每个选择的长度?我找到的方法是$('#mylist option').length
,这在这种情况下似乎不可行,因为我没有每个选择的ID。下面所示的CCD_ 3总是返回"0";1〃;即使存在具有多行的选择。
JS代码:
$('#severalStores').find('.selectLimitInformation').each(function () {
console.log($(this).length); //Always return "1"
});
HTML代码:
<div class="row form-horizontal" id="severalStores">
<div class="col-md-6">
<div class="row form-horizontal">
<div class="col-md-4">
<label>Store AAA:</label>
</div>
<div class="col-md-8">
<select class="form-control selectLimitInformation">
@foreach (...) //data from ViewBag
{
<option value="@storeLimit.info">@storeLimit.description</option>
}
<option value="0">Not available</option>
</select>
</div>
</div>
<div class="row form-horizontal">
<div class="col-md-4">
<label>Store BBB:</label>
</div>
<div class="col-md-8">
<select class="form-control selectLimitInformation">
@foreach (...) //data from ViewBag
{
<option value="@storeLimit.info">@storeLimit.description</option>
}
<option value="0">Not available</option>
</select>
</div>
</div>
//Several other select
</div>
</div>
你差点就搞定了。
首先,循环选择。您不需要在您的案例中使用find。然后在循环中,您确实使用find来查找select的子选项的长度。
$('#severalStores .selectLimitInformation').each(function () {
console.log($(this).find("option").length); //Always return "1"
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row form-horizontal" id="severalStores">
<div class="col-md-6">
<div class="row form-horizontal">
<div class="col-md-4">
<label>Store AAA:</label>
</div>
<div class="col-md-8">
<select class="form-control selectLimitInformation">
<option value="@storeLimit.info">@storeLimit.description</option>
<option value="0">Not available</option>
</select>
</div>
</div>
<div class="row form-horizontal">
<div class="col-md-4">
<label>Store BBB:</label>
</div>
<div class="col-md-8">
<select class="form-control selectLimitInformation">
<option value="@storeLimit.info">@storeLimit.description</option>
<option value="0">Not available</option>
</select>
</div>
</div>
//Several other select
</div>
</div>