0

Please refer following screenshot of Room Types:

Following is my cshtml code part.

       <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>
                }

I want to check whether user enters any of the count value in the above table. I know that I can do it by using a for loop and check if the count has any value. But here I am looking for more easy solution.Can anybody help me?

1 Answers1

1
if ($(".roomCount").filter(function() { return $(this).val(); }).length > 0) {
// value exists.. your code here.. 
}

OR

You can create a common function like the below and would make it reusable:

function hasValue(elem) {
    return $(elem).filter(function() { return $(this).val(); }).length > 0;
}

OR you use each:

$('.roomCount').each(function() {
  var enteredValue = $(this).val();
  if (parseInt(enteredValue) > 0) {
    console.log('Value exist');
  }
});
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35