2017-09-13 115 views
0

我在ASP MVC部分類是這樣工作的:隱藏按鈕不會使用jQuery

<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 [email protected] checked="checked" class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox"> 
      <label [email protected]></label> 
      } else { 
      <input [email protected] class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox"> 
      <label [email protected]></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(); 
} 

當我嘗試通過創建腳本來把它藏在我的局部視圖,它隱藏的按鈕,但爲何它沒有在我的索引視圖工作時,我稱之爲局部視圖?你能告訴我怎麼做對嗎?謝謝。

+0

你在哪裏調用這個函數? – madalinivascu

+0

在我的索引視圖中,放置了我需要隱藏的按鈕的部分視圖。 – Ibanez1700

+0

何時何地調用getVisitorsOfApprovedTicket()方法? –

回答

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

希望幫助