2017-08-03 65 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?