duandeng2265 2015-07-16 14:36
浏览 58
已采纳

jQuery在PC上运行但在WAMP或Shared Server上运行

I don't know why but I have a piece of code which works on my system but is not working on WAMP or Shared server.

Below is my piece of code :-

<html>
  <body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <script type="text/javascript">

      (function($) {

        $.ajax({
          url:"select2.json",
          type:"GET",
          dataType:"json",
          success:function(data){

            var selectedDepartment, selectedSubproj;

            $.fn.changeType = function() {

              var options_projname = '<option>Select<\/option>';
              $.each(data, function(i, d) {
                options_projname += '<option value="' + d.projname + '">' + d.projname + '<\/option>';
              });
              $("select#projname", this).html(options_projname);

              $("select#projname").change(function() {
                var index = $(this).get(0).selectedIndex;

                var d = data[index - 1]; // -1 because index 0 is for empty 'Select' option
                selectedDepartment = d;
                var options = '';
                if (index > 0) {
                  options += '<option>Select<\/option>';
                  $.each(d.subproj, function(i, j) {
                    options += '<option value="' + j.title + '">' + j.title + '<\/option>';
                  });
                } else {
                  options += '<option>Select<\/option>';
                }
                $("select#subproj").html(options);
              });
              $("select#subproj").change(function() {
                var index = $(this).get(0).selectedIndex;
                selectedSubproj = selectedDepartment.subproj[index -1];
                var options = '';
                if (index > 0) {
                  $.each(selectedSubproj.unit, function(i, b) {
                    //options += '<option value="' + b.name + '">' + b.name + '<\/option>';
                    options += '<input type="checkbox" name="' + b.name + '" value="' + b.name + '">' + b.name + '<br/>';
                  });
                } else {
                  options += '<option>Select<\/option>';
                }
                $("#unit").html(options);
              });
            };


          }
        });
      })(jQuery);

      $(document).ready(function() {
        $("form#search").changeType();
      });
    </script>

    <form id="search" action="" name="search">
      <select name="projname" id="projname">
        <option>Select</option>
      </select>

      <select name="subproj" id="subproj">
        <option>Select</option>
      </select>

      <div name="unit" id="unit">

      </div>

    </form>
  </body>
</html>

I am getting the following error when I run in WAMP or Shared Server. http://s8.postimg.org/vj19w76v9/error.png But it runs fine if I run it like a normal html file on my pc. My JSON is also rendering very well so I know that there is no issue with it.

I tried clearing cache and all the good stuff but its eating my brain off...

Would be glad if someone could help.

Thanks in advance... Cheers...

</div>
  • 写回答

1条回答 默认 最新

  • dongxuandong2045 2015-07-16 14:46
    关注

    The problem is that you are defining your changeType plugin in your success callback of the ajax call. Thus, at the time the document is ready, when you call that plugin, it will be undefined. You will need to define the plugin first and think of a way to pass the data variable you get from the ajax call by parameter. I believe something rough like this should work:

    (function ($) {
        $.fn.changeType = function (data) {
            var selectedDepartment, selectedSubproj;
            var options_projname = '<option>Select<\/option>';
            $.each(data, function (i, d) {
                options_projname += '<option value="' + d.projname + '">' + d.projname + '<\/option>';
            });
            $("select#projname", this).html(options_projname);
    
            $("select#projname").change(function () {
                var index = $(this).get(0).selectedIndex;
    
                var d = data[index - 1]; // -1 because index 0 is for empty 'Select' option
                selectedDepartment = d;
                var options = '';
                if (index > 0) {
                    options += '<option>Select<\/option>';
                    $.each(d.subproj, function (i, j) {
                        options += '<option value="' + j.title + '">' + j.title + '<\/option>';
                    });
                } else {
                    options += '<option>Select<\/option>';
                }
                $("select#subproj").html(options);
            });
            $("select#subproj").change(function () {
                var index = $(this).get(0).selectedIndex;
                selectedSubproj = selectedDepartment.subproj[index - 1];
                var options = '';
                if (index > 0) {
                    $.each(selectedSubproj.unit, function (i, b) {
                        //options += '<option value="' + b.name + '">' + b.name + '<\/option>';
                        options += '<input type="checkbox" name="' + b.name + '" value="' + b.name + '">' + b.name + '<br/>';
                    });
                } else {
                    options += '<option>Select<\/option>';
                }
                $("#unit").html(options);
            });
        };
    
    
    })(jQuery);
    
    $(document).ready(function () {
        $.ajax({
            url: "select2.json",
            type: "GET",
            dataType: "json",
            success: function (data) {
                $("form#search").changeType(data);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等