javascript:将脚本添加到.js文件中

时间:2012-07-17 15:40:38

标签: javascript jquery asp.net-mvc

所以对于分页我有这个网址,当用户按下链接时它会转到下一页(链接部分在局部视图中),为了工作我用javascript获取dropdowbox值并传递给网址

 @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文件refence)

所以我刚接触javascript,所以我不知道我是否必须更改它的功能才能在.js文件中使用它在我的所有页面中使用

编辑:

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

1 个答案:

答案 0 :(得分:1)

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

尝试复制此部分:

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

如果你这样做,那么在页面中使用代码与在单独的文件中使用代码之间应该没有区别。