如何使用jQuery在单击按钮时隐藏数据库记录

时间:2019-01-18 11:21:58

标签: javascript jquery asp.net-mvc

我有一个视图,该视图以引导卡的形式获取数据库记录。在每张卡中,我都有与数据库列关联的复选框。 选中复选框并单击“保存”按钮后,我想隐藏带有我在其中输入该值的记录的卡

我该如何实现?我尝试使用卡的类名,但它隐藏了所有卡。

@model IEnumerable
<RoomTypeView>
  <div class="row">
    @foreach (var item in Model) 
    {
      <div class="col-3">
        <div class="card border rounded DropShadow Resbook">
          <div class="card-body">
            Room @Html.DisplayFor(modelItem => item.RoomNo) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="button" class="btn btn-default insert" data-rid="@item.RoomId">Save</button><br /> 
            @Html.DisplayFor(modelItem => item.GuestName)<br />
            <div class="row">
              <div class="col-7 text-center">
                @if (item.Status.HasFlag(EnumHkStatus.Cleaning)) 
                {
                  <input type="checkbox" data-Cleanid="@item.RoomId" name="cstatus" class="form-check-input" />
                  <label>Cleaning</label>
                  <br /> 
                } 
                @if (item.Status.HasFlag(EnumHkStatus.InventoryCheck)) 
                {
                  <input type="checkbox" data-InvChkid="@item.RoomId" name="cstatus" class="form-check-input" />
                  <label>Inventory Check</label> 
                }
              </div>
              <div class="col-5">
                @if (item.NewLinen == null) 
                {
                  <input type="checkbox" data-nlid="@item.RoomId" class="form-check-input" />
                  <label>New Linen</label>
                  <br /> 
                } 
                @if (item.DeepCleaning == null) 
                {
                  <input type="checkbox" data-dcid="@item.RoomId" class="form-check-input" />
                  <label>Deep Cleaning</label> 
                }
              </div>
            </div>
            <div class="row">
              <div class="col-8">
                <div class="inventory my-1">
                  <textarea class="form-control breakage" placeholder="Enter Breakage Note" rows="1"></textarea>
                </div>
              </div>
              <div class="col-4">
                <button type="button" class="btn btn-default breakage" data-brid="@item.ReservationID">
                  <i class="fa fa-file-invoice" style="color:red;"></i>
                </button>
              </div>
            </div>
            <div class="row">
              <div class="col-8">
                <div class="comments my-1">
                  <textarea class="form-control inventoryms" placeholder="Enter Inventory Missing" rows="1"></textarea>
                </div>
              </div>
              <div class="col-4">
                <button type="button" class="btn btn-default invmissing" data-invmid="@item.ReservationID">
                  <i class="fa fa-file-invoice" style="color:red;"></i>
                </button>
              </div>
            </div>
            @Html.DisplayFor(modelItem => item.Comments)
          </div>
        </div>
      </div>
    }
  </div>
$('.Resbook').hide();

rId隐藏了所有卡片。如何克服呢?我如何使用location来隐藏该特定卡?

1 个答案:

答案 0 :(得分:1)

  

它隐藏了所有卡片

您需要确保仅隐藏相关的卡。 .insert按钮位于.card中,因此您可以使用.card找到.closest,然后使用相对查找。

var card = $(this).closest(".card");
$('input[data-invchkid=' + rId + ']:checked', card)

card.find('input[data-invchkid=' + rId + ']:checked')

由于.card也是.Resbook<div class='card Resbook'>),因此您可以使用.Resbook代替.card,例如:

var card = $(this).closest(".Resbook");
$(card).hide();

给予:

$('.insert').click(function() {
  var card = $(this).closest(".card");
  var rId = $(this).data('rid');

  $.post("@Url.Action("
    SetCleaningStatus ", "
    HouseKeeping ")", {
      id: rId,
      InvChk: $('input[data-invchkid=' + rId + ']:checked', card).length,
      ClnChk: $('input[data-cleanid=' + rId + ']:checked', card).length,
      NewLin: $('input[data-nlid=' + rId + ']:checked', card).length,
      DpClean: $('input[data-dcid=' + rId + ']:checked', card).length
    });

  $(card).hide();
});

示例片段显示了通过OPs结构使用.closest()

$(".insert").click(function() {
  $(this).closest(".card").hide();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="col-3">
    <div class="card Resbook">
      <div class="card-body">
        Room 1
        <button type="button" class="btn btn-default insert" data-rid="@item.RoomId">Save</button>
      </div>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-3">
    <div class="card Resbook">
      <div class="card-body">
        Room 2
        <button type="button" class="btn btn-default insert" data-rid="@item.RoomId">Save</button>
      </div>
    </div>
  </div>
</div>


如果按钮不在要使用的卡片层次结构内,则可以通过其data-rid=值将它们链接在一起。

.data-rid添加到.card以便它们匹配:

<div class='card' data-rid='@item.RoomId'/>

然后将两者与

匹配
$("button").click(function() {
    var rid = $(this).data("rid");
    var card = $(".card[data-rid=" + rid + "]")

示例片段:

$(".external-insert").click(function() {
  var rid = $(this).data("rid")
  var card = $(".card[data-rid=" + rid + "]")
  card.hide();
  $(this).fadeOut(); // also hide the button
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="col-3">
    <div class="card Resbook" data-rid="1">
      <div class="card-body">
        Room 1
      </div>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-3">
    <div class="card Resbook" data-rid="2">
      <div class="card-body">
        Room 2
      </div>
    </div>
  </div>
</div>
<hr/>
<button type="button" class="btn btn-default insert external-insert" data-rid="1">Save 1</button>
<button type="button" class="btn btn-default insert external-insert" data-rid="2">Save 2</button>