使用jquery隐藏按钮不起作用

时间:2017-09-13 03:56:24

标签: javascript jquery

我在Asp MVC中有一个部分类,如下所示:

<div class="table-responsive">
  <table class="table" id="tbl_visitors" data-animate="fadeInRight">
    <thead>
      <tr>
        <th>
          @Html.DisplayNameFor(model => model.VisitorId)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.TicketId)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.RequestStatus)
        </th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      @foreach (var item in Model) {
      <tr>
        <td>
          @Html.DisplayFor(modelItem => item.VisitorId)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.TicketId)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.RequestStatus)
        </td>
        <td id="item_checkbox">
          @if (item.RequestStatus != "Pending") {
          <input id=@item.VisitorId checked="checked" class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox">
          <label for=@item.VisitorId></label>
          } else {
          <input id=@item.VisitorId class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox">
          <label for=@item.VisitorId></label>
          }
        </td>
        <th>
          <a href="javascript:void(0);" onclick="deletevisitor(@item.VisitorId, @item.TicketId ) ">Remove</a>
        </th>
      </tr>
      }
    </tbody>
  </table>
</div>
<div class="alignright" id="div_btn_approve">
  <button id="btn_approve_ticket" class="button button-border button-rounded button-green">Approve</button>
</div>

和一个脚本,在我的“索引”视图中禁用部分视图中的按钮,如下所示:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  $("#div_approved_ticket_visitors").load(url);
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
  $("#btn_approve_ticket").hide();
}

当我尝试通过创建脚本在部分视图中隐藏它时,它隐藏了按钮,但是为什么当我调用局部视图时它不能在我的索引视图上工作?你能告诉我怎么做对吗?谢谢。

1 个答案:

答案 0 :(得分:2)

由于.load尚未完成,您尝试.hide()一个不存在的元素:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  // this is a async process
  $("#div_approved_ticket_visitors").load(url);
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
  // you are trying to hide an element that is not present yet.
  $("#btn_approve_ticket").hide();
}

如果要隐藏此var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;视图中的元素。在继续之前,您需要先等待它加载:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  // this is a async process
  $("#div_approved_ticket_visitors").load(url, function () {
    // this function is called after its done
    // here you can safely hide the element
    $("#btn_approve_ticket").hide();
  });
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
}

希望有所帮助