1

因此,对于分页,我有这个 url,当用户按下链接时,它会转到下一页(链接部分在部分视图中),为此,我使用 javascript 获取 dropdowbox 值并传递给 url

 @Html.ActionLink("|Siguiente >", "Index", new { pagina = Model.PageNumber + 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa" }, new { id = "mylinkSig" })



<script type="text/javascript">
     $(function () {
            $('#mylinkSig').click(function () {
                var empresa = $("#empresa").val();
                this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
            });
        });
</script> 

当我在几乎所有页面中使用该脚本时,我想将此脚本放入 js 文件中,因此我不必将其写入所有视图(页面)中您尝试复制/粘贴并放入 js 文件中,但它不起作用(是的,我的页面中有 .js 文件引用)

所以我是 javascript 的新手,所以我不知道是否必须更改函数,因为它可以在 .js 文件中工作并在我的所有页面中使用

编辑:

我的 Helper.js

$(function () {
    $('#mylinkSig').click(function () {
        var empresa = $("#empresa").val();
        this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
    });
});


function deleteConfirmation(response, status, data) {

    // remove the row from the table
    var rowId = "#widget-id-" + response.id;
    $('.widgets').find(rowId).remove();

    // display a status message with highlight
    $('#actionMessage').text(response.message);
    $('#actionMessage').effect("highlight", {}, 3000);
    if (response.message == null) {
        alert('No se pudo eliminar el registro');
    }
    else {
        alert(response.message);
    }

}

我的观点

<script src="@Url.Content("~/Scripts/Helper.js")" type="text/javascript"></script>

&nbsp;
@*navigation << < >>>*@
<div>

    Pagina @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    de @Model.PageCount
    &nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<|", "Index", new { pagina = 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro, empresa = "param-empresa" }, new { id = "mylinkFirst" })
        @Html.Raw("&nbsp;");
        @Html.ActionLink("< Anterior|", "Index", new { pagina = Model.PageNumber - 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro, empresa = "param-empresa" }, new { id = "mylinkAnt" })
    }
    &nbsp;
    @if (Model.HasNextPage)
    {
        @Html.ActionLink("|Siguiente >", "Index", new { pagina = Model.PageNumber + 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa" }, new { id = "mylinkSig" })
        @Html.Raw("&nbsp;");
        @Html.ActionLink("|>>", "Index", new { pagina = Model.PageCount, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa"}, new { id = "mylinkLast" })
    }

</div>

&nbsp;
4

1 回答 1

1

如果您有对该文件的引用,那么我最好的猜测是您也将script-tag 复制到 JS 文件中,这是您不应该的。

试着复制这部分:

$(function () {
    $('#mylinkSig').click(function () {
        var empresa = $("#empresa").val();
        this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
    });
});

如果这样做,则将代码放在页面中与将代码放在单独的文件中相比应该没有区别。

于 2012-07-17T15:44:48.503 回答