JQuery MVC4 $(document).ready(函数未被执行

时间:2015-10-16 17:11:55

标签: javascript jquery asp.net-mvc-4

我有一个复杂的MVC4页面,已经多次使用$(document).ready

一切都运作良好。

但是在这种特殊情况下,我可以在Firebug中逐步看到光标直接跳到最后一个大括号。以下是代码:

<script type="text/javascript">
        $(document).ready(function(){
            var ProjectId = GetURLParameter('ProjectId');
            var ContextType = GetURLParameter('ContextType');
            var ContextId = GetUrlParameter('ContextId');
            var Category = GetUrlParameter('category');
            var Schmurz = GetURLParameter('schmurz');
            if (Category)
            {
                var strMethodUrl = "/Tree/GetSelectedCategory?category=" + Category;
                $.get(strMethodUrl, receiveResponse);
            }

            function GetURLParameter(sParam) {
                var sPageURL = window.location.search.substring(1);
                var sURLVariables = sPageURL.sp('&');
                if (sURLVariable) {
                    for (var i = 0; i < sURLVariables.length; i++) {
                        var sParameterName = sURLVariables[i].split('=');
                        if (sParameterName[0] == sParam) {
                            return sParameterName[1];
                        }
                    }
                }

                return '';
            }
        });               
</script>

如果我按如下方式写这个,那就完全相同了:

 <script type="text/javascript">
        $(document).ready(function(){
            var ProjectId = GetURLParameter('ProjectId');
            var ContextType = GetURLParameter('ContextType');
            var ContextId = GetUrlParameter('ContextId');
            var Category = GetUrlParameter('category');
            var Schmurz = GetURLParameter('schmurz');
            if (Category)
            {
                var strMethodUrl = "/Tree/GetSelectedCategory?category=" + Category;
                $.get(strMethodUrl, receiveResponse);
            }            
        });

        function GetURLParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.sp('&');
            if (sURLVariable) {
                for (var i = 0; i < sURLVariables.length; i++) {
                    var sParameterName = sURLVariables[i].split('=');
                    if (sParameterName[0] == sParam) {
                        return sParameterName[1];
                    }
                }
            }

            return '';
        }
</script>

我将非常感谢您的帮助,因为我真的不明白这个原因,并且需要这段代码很快就能使用。

谢谢!

1 个答案:

答案 0 :(得分:0)

函数调用区分大小写。即使您已定义GetURLParameter,也在调用GetUrlParameter。还要确保所有sURLVariables都正确命名。

<script type="text/javascript">
    $(document).ready(function(){

         function GetURLParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.sp('&');
            if (sURLVariables) {
                for (var i = 0; i < sURLVariables.length; i++) {
                    var sParameterName = sURLVariables[i].split('=');
                    if (sParameterName[0] == sParam) {
                        return sParameterName[1];
                    }
                }
            }

            return '';
        }

        var ProjectId = GetURLParameter('ProjectId');
        var ContextType = GetURLParameter('ContextType');
        var ContextId = GetURLParameter('ContextId');
        var Category = GetURLParameter('category');
        var Schmurz = GetURLParameter('schmurz');


        if (Category)
        {
            var strMethodUrl = "/Tree/GetSelectedCategory?category=" + Category;
            $.get(strMethodUrl, receiveResponse);
        }


    });               

就定义函数的时间而言,这两种方法都有效:

http://jsfiddle.net/6qavvth8/1/

http://jsfiddle.net/6qavvth8/2/