2009-07-22 5 views
0

私はこれ持っていた、前に:のJavascriptファイル参照(jQueryを使って)問題

<head> 
    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     var optPrompt = "- Select One -"; 
     var subCats; 
     var parentCats; 
     var nextBtn; 

     var ParentChanged = function() { 
      ClearDescription(); 

      if (this.selectedIndex == 0) { 
       $(subCats).html($("<option>").text(optPrompt)); 
      } 


      $.getJSON('<%=Url.Action("GetChildCategories") %>', { parentId: $(this).val() }, 
        function(data) { 
         subCats.options.length = 0; 
         $("<option>").text(optPrompt).appendTo(subCats); 
         $(data).each(function() { 
          $("<option>").attr("value", this.ID).text(this.Name).appendTo(subCats); 
         }); 
        }); 
     } 

     var DisplayDescription = function(catId) { 
      $.ajax({ 
       url: '<%=Url.Action("GetDescription") %>', 
       data: { categoryId: catId }, 
       dataType: 'html', 
       success: function(data) { 
        $("p#categoryDescription").html(data); 
       } 
      }); 
     } 

     var ChildChanged = function() { 
      var catSelected = this.selectedIndex != 0; 

      if (!catSelected) ClearDescription(); 
      else DisplayDescription($(this).val()); 
     } 

     var ClearDescription = function() { 
      $("p#categoryDescription").html(''); 
     } 

     $(function() { 
      parentCats = $("select#Category").get(0); 
      subCats = $("select#Subcategory").get(0); 
      nextBtn = $("input#nextButton").get(0); 

      $(parentCats).change(ParentChanged); 
      $(subCats).change(ChildChanged); 
     }); 

    </script> 
</head> 

は、その後、私は(myScript.js)ファイルに私のインラインスクリプトのすべてを入れて、これに私のHTMLを変更:

<head> 
    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script src="/Scripts/myScript.js" type="text/javascript"></script> 
</head> 

これで何も問題はありません。私はIE7で自分のページを開いて、それが読み出されたページエラーが発生しました:

Line: 54
Error: Unknown name.

ライン54は私の外部のJavaScriptファイルの最後の行であることを起こります。

私は間違っていますか?

答えて

4

私はこれがASP.Netだと言っているのでしょうか?そうであれば、インラインスクリプトは次のようになります。

<%=Url.Action("GetDescription") %> 

外部JavaScriptファイルには書き込めません。

+0

これは問題です.JavaScriptリファレンス内のASP.NETコードは、サーバー上のASP.NETで処理されません。 –

+0

私はそれが問題だと思う。私はそれを知っていましたが、私はそれをスクリプトに入れていたことを忘れていました。ああ、私はちょうどそのページにスクリプトを残すだろうと思う。私は他に何かできるとは思わない。 –

2

< script>タグをmyScript.jsに入れましたか?はいの場合は削除します。

あなたmyScript.jsが

のvar optPromptで始めるべき= " - ワンを選択してください - "。

+0

いいえ私は間違いをしなかった。 –

2

あなたは今ではなく、あなたのASP経由よりも、静的ファイルとしてJSを提供しているので、サーバーがそれらを解釈し、正しい値とそれらを置き換えるものではありませんので、

<%=Url.Action("GetChildCategories") %> 

のような行が機能しなくなります。スクリプト内でそれらをハードコーディングするか、メインページにインラインスクリプトとしてその行を残し、それらを外部変数から参照できるグローバル変数として設定する必要があります。