页面在加载时不运行jquery脚本

时间:2009-09-22 18:25:14

标签: jquery asp.net-mvc

我有一个ASP.net mvc页面,在加载时执行jquery脚本。 该脚本调用控制器上的操作并为下拉列表提供保湿。

这适用于我的开发机器但是当部署到网络服务器(运行IIS 6的Win 2k3盒子)时,页面会加载,但它不会运行脚本,从而产生一个空的下拉列表。

我在scripts文件夹中有jquery-1.3.2.js文件,我在webserver上添加了aspnet_isapi.dll的映射。还有什么我想念的吗?

这是页面的一部分,它可以保存在我的计算机上运行的下拉列表,但不会在它部署到的Web服务器上,因为您可以看到脚本调用ApplicationSettings控制器以获取一个JSON对象来保护drop下面的列表

    <asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        // Wait for the document to be ready
        $(document).ready(function()
        {
            var selectedApp = $('#selectedApplication').val();
            var selectedMac = $('#selectedMachine').val();

            // Get the list of applications and populate the applications drop down list
            $.getJSON("/ApplicationSettings/Applications/List", function(data)
            {
                var items = "<option>----------- Select Application to Configure ----------</option>";
                $.each(data, function(i, application)
                {
                    var selected = (application.Value == selectedApp) ? 'selected' : '';
                    items += "<option value='" + application.Value + "'" + selected + ">" + application.Text + "</option>";
                });
                $("#Applications").html(items);
            });

            // Get the list of machines where the selected application is installed and populate the machines drop down list
            $("#Applications").change(function()
            {
                if ($("#Applications").attr("value") != "")
                {
                    // Enable the Machines DDL if a valid application is selected
                    $("#Machines").removeAttr("disabled");

                    // Populate the machines DDL with a list of machines where the selected application is installed
                    $.getJSON("/ApplicationSettings/Machines/List/" + $("#Applications > option:selected").attr("value"), function(data)
                    {
                        // Set the first item in the list
                        var items = "<option>---------- Select Machine -----------</option>";

                        // Retrieve the list of machines for th selected application from the database
                        $.each(data, function(i, machine)
                        {
                            var selected = (machine.Value == selectedMac) ? 'selected' : '';
                            items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>";
                        });

                        // Add the items retrieved to the Machines DDL
                        $("#Machines").html(items);

                        if ($("#Machines").attr("value") != "")
                        {
                            $("#btnSearch").removeAttr("disabled");
                        }
                        else
                        {
                            $("#btnSearch").attr("disabled", "disabled");
                        }
                    });
                }
                else
                {
                    // If a valid application has not been selected then disable the Machines DDL
                    $("#Machines").attr("disabled", "disabled");
                    $("#btnSearch").attr("disabled", "disabled");
                }
            });

            if (selectedApp != "")
            {
                $("#Machines").removeAttr("disabled");

                $.getJSON("/ApplicationSettings/Machines/List/" + selectedApp, function(data)
                {
                    var items = "<option>---------- Select Machine -----------</option>";
                    $.each(data, function(i, machine)
                    {
                        var selected = (machine.Value == selectedMac) ? 'selected' : '';
                        items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>";
                    });
                    $("#Machines").html(items);
                });

                if (selectedMac != "")
                {
                    $("#btnSearch").removeAttr("disabled");
                }
                else
                {
                    $("#btnSearch").attr("disabled", "disabled");
                }
            }
            else
            {
                $("#Machines").attr("disabled", "disabled");
                $("#btnSearch").attr("disabled", "disabled");
            }
        });


        function saveSelectedApplication()
        {
            $("#selectedApplication").val("");
            $("#selectedMachine").val("");
            $("#selectedApplication").val($("#Applications").attr("value"));
            if ($("#Applications").attr("value") != "")
            {
                $("#Machines").removeAttr("disabled");
                if ($("#Machines").attr("value") != "")
                {
                    $("#btnSearch").removeAttr("disabled");
                }
                else
                {
                    $("#btnSearch").attr("disabled", "disabled");
                }
            }
            else
            {
                $("#Machines").attr("disabled", "disabled");
                $("#btnSearch").attr("disabled", "disabled");
            }
        }

        function saveSelectedMachine()
        {
            $("#selectedMachine").val("");
            $("#selectedMachine").val($("#Machines").attr("value"));
            if ($("#Machines").attr("value") != "")
            {
                $("#btnSearch").removeAttr("disabled");
            }
            else
            {
                $("#btnSearch").attr("disabled", "disabled");
            }
        }
    </script>

3 个答案:

答案 0 :(得分:5)

我遇到了脚本路径问题。我使用这种扩展方法对它进行排序。

public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Scripts the specified HTML to allow for correct pathing of the resource.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string Script(this HtmlHelper html, string path)
        {
            var filePath = VirtualPathUtility.ToAbsolute(path);
            return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
        }
    }

然后把它放在母版页中:

<%@ Import Namespace="MYNAMESPACE.Helpers" %>

然后jsut注册所有脚本,如:

<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%>

尝试实现以下帮助器:

public static string AbsolutePath(this HtmlHelper html, string path)
{
    return VirtualPathUtility.ToAbsolute(path);
}

然后将您的通话更改为

$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>"

渲染视图时,MVC ViewEngine应插入绝对路径。

答案 1 :(得分:0)

您将代码放入“$(document).ready(function(){[YOUR_CODE]});”块?如果没有,DOM可能还没有准备好;)

答案 2 :(得分:0)

你是如何编码的?如果您在服务器上的虚拟目录中运行,这可能很重要。如果您在http://localhost:xxxx/controller/action本地运行,但以http://mysever/myapp/controller/action远程运行​​,那么您需要确保使用Url.Action()来解析操作结果的真实路径。

相关问题