请参阅以下房间类型的屏幕截图:
以下是我的cshtml代码部分。
<div class="hotelRoomInfo">
@for (int i = 0; i < 6; i++)
{
var catId = "roomcatID_" + i;
var typeId = "roomtypeID_" + i;
var countId = "roomcountID_" + i;
var id = "roomID_" + i;
var item = ((List<string>)ViewBag.RoomTypeList)[i];
<div class="row">
<div class="col-md-6">
<div class="col-md-4">
<label class="roomType" id="@typeId">@item</label>
</div>
<div class="col-md-4">
@Html.DropDownList(string.Format("{0}", catId), ((IEnumerable<SelectListItem>)ViewBag.RoomCatList), new { @class="roomCategory"})
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="roomCount" id="@countId" value="0"/>
</div>
</div>
<input type="hidden" id="@id" value="0">
</div>
<div class="col-md-6"></div>
</div>
}
我想检查用户是否输入上表中的任何计数值。我知道我可以通过使用for循环来检查计数是否有任何值。 但在这里,我正在寻找更简单的解决方案。任何人都可以帮助我吗?
答案 0 :(得分:1)
if ($(".roomCount").filter(function() { return $(this).val(); }).length > 0) {
// value exists.. your code here..
}
OR
您可以创建如下所示的常用功能,并使其可重复使用:
function hasValue(elem) {
return $(elem).filter(function() { return $(this).val(); }).length > 0;
}
或者您使用each
:
$('.roomCount').each(function() {
var enteredValue = $(this).val();
if (parseInt(enteredValue) > 0) {
console.log('Value exist');
}
});