Window.location不适用于javascript

时间:2017-08-03 13:43:14

标签: javascript jquery

我需要在按钮点击时使用javascript重定向到另一个视图。我需要使用javascript的原因是因为我需要我点击的行的“Account Id”。以下是我的代码

这是为了呈现页面

<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>

这是将我导航到另一个视图的javascript

 $('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)时,“我得到了正确的网址"/ViewAccountDetail/AccountVehicle?acctId=7"

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

尝试将location.href设置为新网址,例如:

location.href = '/'

答案 1 :(得分:0)

window.location.href = url;将有效

答案 2 :(得分: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?