2013-11-25 29 views
0

調用所有JavaScript專家。我要去香蕉,在下面的腳本中,如果我刪除警報代碼不起作用。但我顯然不能在我的ASP.NET web應用程序中留下彈出窗口Javascript的問題,腳本工作100%只有警報(); ODD

並不重要,但此代碼位於包含稱爲IndexSearch Results.cshtml的部分的Index.cshtml中。在底部,但它只是JavaScript的下方,我認爲代碼是問題::

有遺漏我弄不明白,許多感謝尋找...

@section Scripts { 
    @Scripts.Render("~/bundles/tablesorter") 

    <script type="text/javascript"> 





     $(document).ready(function() { 




      $('a').each(function() { 
       $(this).qtip({ 
        content: { 
         text: $(this).next('.tooltiptext') 
        } 
       }); 
      }); 




      $('#searchButton').click(function() { 
       var url = '/SupplyPoint/IndexSearch'; 

       var data = { 
        searchSPID: $('#SearchSPID').val().toString(), 
        searchPremise: $('#SearchPremise').val().toString() 
       }; 

       $("#ResultsList").load(url, data, function() { 
        $('#LoadingGif').empty(); 
       }); 


       $('#LoadingGif').empty().html('&nbsp;&nbsp;&nbsp;Loading...'); 

       Init(); 

      }); 


      function Init() { 

       if ($("#myTable").find("tr").size() > 1) { 

        alert('Without this Alert the Tablesorter does not apply formatting, i.e. no paging, no zebra, nothing just one big list, odd'); 
        $("#myTable").tablesorter({ dateFormat: "uk", widgets: ['zebra'], sortList: [[4, 0]] }).tablesorterPager({ container: $("#pager") }); 

        $('tr').live('click', function (e) { 

         //if not clicking an anchor tag or imag then assume user wants to go to details page 
         if ((!$(e.target).is('a')) && (!$(e.target).is('img')) && (!$(e.target).is('th')) && !(e.target.isTextEdit)) { 
          if ($(this).attr('rowid') != null) 
           window.location = 'SupplyPoint/Details/' + $(this).attr('rowid'); 
         } 

        }); 
       } else { 

        $("#myTable").hide(); $("#pager").hide(); 
       } 

       return false; 
      }; 




     }); 


    </script> 

這裏是Index.CSHTML:

@model IEnumerable<AscendancyCF.Models.SupplyPoint> 

@{ 
    ViewBag.Title = "Index"; 
} 


<style> 
    .tooltiptext { 
     display: none; 
    } 
</style> 

<h2></h2> 

      <fieldset> 
       <legend> 
        <img width="50" height="50" src="~/content/images/bPremise.jpg" />&nbsp;&nbsp;Use the Search fields provided to Locate a Premise 

       </legend> 

       <table class="NoHighlight"> 
        <tr> 
         <td></td> 
         <td> 
          SSID<br /> 

           @Html.TextBox("SearchSPID", string.Empty, new { style = "float:left;width:150px;" }) 




          <div style="clear:both;"></div> 
         </td> 
         <td></td> 
         <td> 
          Premise Name<br /> 
          @Html.TextBox("SearchPremise", string.Empty, new { style = "float:left;width:170px;" }) 
          <div id="Div1" style="float:left; padding-left:5px;"></div> 
          <div style="clear:both;"></div> 
         </td> 
         @*<td> 
          Effective Start >=<br /> 
          <div class="col-md-10"> 
           @Html.TextBox("StartDate") 
          </div> 
         </td> 
         <td> 
          Effective End<br /> 
          <div class="col-md-10"> 
           @Html.TextBox("EndDate") 
           </div> 
         </td>*@     

         <td> 

          <a href="#test" class="btn btn-default" id="searchButton" >Search</a> 

          <div class="tooltiptext"> 
           Key less by entering only part of a Search String <b>i.e.</b> key <i>Garage</i> to find <i>all premises containing this text</i> quickly 
          </div> 

         </td> 
         <td> 
          <div id="LoadingGif"></div> 


         </td> 

        </tr> 
       </table> 
      </fieldset> 

      <div id="ResultsList" style="clear:both;"> 
       @Html.Partial("IndexSearchResults") 



      </div> 


@section Scripts { 
    @Scripts.Render("~/bundles/tablesorter") 

    <script type="text/javascript"> 





     $(document).ready(function() { 




      $('a').each(function() { 
       $(this).qtip({ 
        content: { 
         text: $(this).next('.tooltiptext') 
        } 
       }); 
      }); 




      $('#searchButton').click(function() { 
       var url = '/SupplyPoint/IndexSearch'; 

       var data = { 
        searchSPID: $('#SearchSPID').val().toString(), 
        searchPremise: $('#SearchPremise').val().toString() 
       }; 

       $("#ResultsList").load(url, data, function() { 
        $('#LoadingGif').empty(); 
       }); 


       $('#LoadingGif').empty().html('&nbsp;&nbsp;&nbsp;Loading...'); 

       Init(); 

      }); 


      function Init() { 

       if ($("#myTable").find("tr").size() > 1) { 

        alert('Without this Alert the Tablesorter does not apply formatting, i.e. no paging, no zebra, nothing just one big list, odd'); 
        $("#myTable").tablesorter({ dateFormat: "uk", widgets: ['zebra'], sortList: [[4, 0]] }).tablesorterPager({ container: $("#pager") }); 

        $('tr').live('click', function (e) { 

         //if not clicking an anchor tag or imag then assume user wants to go to details page 
         if ((!$(e.target).is('a')) && (!$(e.target).is('img')) && (!$(e.target).is('th')) && !(e.target.isTextEdit)) { 
          if ($(this).attr('rowid') != null) 
           window.location = 'SupplyPoint/Details/' + $(this).attr('rowid'); 
         } 

        }); 
       } else { 

        $("#myTable").hide(); $("#pager").hide(); 
       } 

       return false; 
      }; 




     }); 


</script> 

}

這裏是IndexSearchResults.cshtml:

@model IEnumerable<AscendancyCF.Models.SupplyPoint> 

@{ 
    ViewBag.Title = "Index"; 
} 

<p><h5><div id="noRecords">Your search returned @Model.Count() items</div></h5></p> 
<table class="tablesorter" id="myTable"> 
    <thead> 
     <tr> 

      <th></th> 
      <th> 
       @Html.DisplayNameFor(model => model.SPID) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.SupplyPointName) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.GazateerRef) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.SupplyPointEffectiveDateTime) 
      </th> 
      <th> 
       Premise Type 
      </th> 

     </tr> 
    </thead> 
    <tbody> 
     @foreach (var item in Model) 
{ 

    <tr rowid="@item.SupplyPointId"> 
     <td> 
      <a href="/SupplyPoint/Edit/@item.SupplyPointId" id="Edit"><img src="~/Content/images/edit.png" id="imgEdit" alt='Edit Details' title='Edit Details'></a> 


     </td> 


     <td> 
      @Html.DisplayFor(modelItem => item.SPID) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SupplyPointName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.GazateerRef) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SupplyPointEffectiveDateTime) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SupplyPointType.SupplyPointTypeName) 
     </td> 

    </tr> 

} 
    </tbody> 
</table> 

<div class="pager" id="pager"> 

    <form> 
     <img src="~/Content/Images/first.gif" id="iFirst" class="first" /> 
     <img src="~/Content/Images/prev.gif" class="prev" /> 
     <input type="text" class="pagedisplay" /> 
     <img src="~/Content/Images/next.gif" class="next" /> 
     <img src="~/Content/Images/last.gif" class="last" /> 
     <select class="pagesize"> 
      <option selected="selected" value="10">10</option> 
      <option value="20">20</option> 
      <option value="30">30</option> 
      <option value="100">100</option> 
      <option value="500">500</option> 
     </select> 
    </form> 
</div> 
+0

我建議你在啓動時聲明一個tablesorter實例,而不是在click函數中。 – vogomatix

回答

1

jQuery.load是異步的,所以它會在加載內容之前返回。在Init函數中,您正嘗試在加載之前使用該內容。 alert()使它成功,因爲它允許加載內容。

而不是在load()調用後立即呼叫Init(),您應該在回調中移動它。

$('#searchButton').click(function() { 
    var url = '/SupplyPoint/IndexSearch'; 

    var data = { 
     searchSPID: $('#SearchSPID').val().toString(), 
     searchPremise: $('#SearchPremise').val().toString() 
    }; 

    $("#ResultsList").load(url, data, function() { 
     $('#LoadingGif').empty(); 
     Init(); // <--- Here 
    }); 


    $('#LoadingGif').empty().html('&nbsp;&nbsp;&nbsp;Loading...'); 
}); 
+0

令人驚訝的是,你是與該!工作,太靠近了,我猜 - 現在喝啤酒 – John