2017-08-03 64 views
-1

我需要重定向到另一個使用按鈕點擊javascript的視圖。我需要使用JavaScript的原因是因爲我需要我點擊的行的「Account Id」。下面是我的代碼Window.location不能在JavaScript上工作​​

這是爲了渲染頁面

<link rel="stylesheet" type="text/css" 

href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css"> 
<link href="@Url.Content("~/css/bootstrap.css")" rel="stylesheet"> 
<link href="~/Content/themes/base/all.css" rel="stylesheet" /> 
<script> 
    $(document).ready(function() { 
     $("#acctInfo").DataTable(); 
     //$(".td_0").hide(); 
    }); 
</script> 

<!-- Page Title 
    ============================================= --> 
<section id="page-title"> 

    <div class="container clearfix"> 
     <h1>View Accounts</h1> 
    </div> 
</section><!-- #page-title end --> 


<section id="content"> 
    <div class="content-wrap"> 
     <div class="container clearfix"> 
      <table class="table table-striped table-condensed table-hover" id="acctInfo"> 
       <thead> 
       <tr> 
        <th class="td_0">Account Id</th> 
        <th>Employee Id</th> 
        <th>First Name</th> 
        <th>Middle Name</th> 
        <th>Last Name</th> 
        <th>Business Name</th> 
        <th>Account Type</th> 
        <th></th> 
        <th></th> 
        <th></th> 
       </tr> 
       </thead> 
       <tbody> 
       @foreach (var i in Model.AccountsViews) 
       { 
        <tr id="rowx"> 
         <td> @Html.DisplayFor(m => i.AcctId)</td> 
         <td> @Html.DisplayFor(m => i.EmployeeId)</td> 
         <td> @Html.DisplayFor(m => i.FirstName)</td> 
         <td> @Html.DisplayFor(m => i.MiddleName)</td> 
         <td> @Html.DisplayFor(m => i.LastName)</td> 
         <td> @Html.DisplayFor(m => i.BusinessName)</td> 
         <td> @Html.DisplayFor(m => i.AccountType)</td> 
         <td><a href="javascript:void(0)" class="lnkDetail btn btn-success form-control">Detail</a></td> 
         <td><a href="javascript:void(0)" class="lnkEdit btn btn-success form-control">Edit</a></td> 
         <td><a href="javascript:void(0)" class="lnkDelete btn btn-danger form-control">Delete</a></td> 

         @*<td> 
           <input type="button" value="Edit" class="btn btn-success form-control" onclick="EditSelected(this)"> 
           <input id="btn_edit[]" type="button" value="Edit" class="btn btn-success form-control" /> 
          </td> 
          <td> 
           <input type="button" value="Delete" class="btn btn-danger btn-success form-control" /> 
          </td>*@ 
        </tr> 
       } 
       </tbody> 
      </table> 
     </div> 
    </div> 
</section> 

<div id="divEdit" style="display: none;"> 

    <input type="hidden" id="hidId"/> 
    <table> 
     <tr> 
      <td>Employee Id</td> 
      <td><input type="text" id="txtEmployeeId" class="form-control"/></td> 
     </tr> 
     <tr> 
      <td>First Name</td> 
      <td><input type="text" id="txtFirstName" class="form-control"/></td> 
     </tr> 
     <tr> 
      <td>Middle Name</td> 
      <td><input type="text" id="txtMiddleName" class="form-control"/></td> 
     </tr> 
     <tr> 
      <td>Last Name</td> 
      <td><input type="text" id="txtLastName" class="form-control"/></td> 
     </tr> 
     <tr> 
      <td>Business Name</td> 
      <td><input type="text" id="txtBusinessName" class="form-control"/></td> 
     </tr> 
     <tr> 
      <td>Account Type</td> 
      <td> 
       @Html.DropDownList("ddlAccount", new List<SelectListItem> 
       { 
        new SelectListItem{Text = "Supplier", Value = "Supplier"}, 
        new SelectListItem{Text = "Employee", Value = "Employee"} 
       }, new{@class="form-control", id="ddlAccount"}) 
      </td> 
     </tr> 
    </table> 
</div> 

這是導航我另一種觀點認爲

$('a.lnkDetail').on("click", function() { 

     var row = $(this).closest('tr'); 
     var acctId = row.find("td:eq(0)").html().trim(); 
     var url = '@Url.Action("ViewAccountDetail", "AccountWVehicle")?acctId=' + acctId; 

     Window.location = url; 


     return false; 
    }); 

當我使用alert(url)的JavaScript「我得到像這樣的正確網址"/ViewAccountDetail/AccountVehicle?acctId=7"

非常感謝您的幫助。

+0

你從來沒有真正window.location'設置'到新網址。 –

+0

你不會對你的網址做任何事情。如果你不想設置它,是什麼讓你覺得'window.location'不工作? – DarthJDG

+0

你是什麼意思達思?只要我調用動作「ViewAccountDetail」,我應該導航到它的'視圖。 – Ibanez1408

回答

2

嘗試設置location.href到新的URL,例如:

location.href = '/' 
+1

這個伎倆! Window.location = url //不起作用Window.location.href //不起作用,但是location.href起作用!謝謝!我儘可能地點擊支票。我需要等待6分鐘。 – Ibanez1408

0

試試這個。

$('a.lnkDetail').on("click", function (event) { 
    event.preventDefault(); 

    var row = $(this).closest('tr'); 
    var acctId = row.find("td:eq(0)").html().trim(); 
    window.location = '@Url.Action("ViewAccountDetail", "AccountWVehicle")?acctId=' + acctId; 
}); 

此處瞭解詳情:What is the difference between Window and window?