2011-10-24 13 views
0

:Index.cshtmlに続いてdocument.readyのjqueryの難易度

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>@ViewBag.Title</title> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.css")" rel="stylesheet" type="text/css" /> 
    <script src="@Url.Content("http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.js")" type="text/javascript"></script> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
</head> 
<body> 
    @RenderBody() 
</body> 
</html> 

を私は次のコードをしている:私はFirebugのと

@{ 
    ViewBag.Title = "Index"; 
} 
<h2> 
    Index</h2> 
<div data-role="page"> 
    <div data-role="header"> 
     ...</div> 
    <div data-role="content"> 
     <a id="btnShowCustomers" data-role="button" href="#secondDiv"></a> 
    </div> 
    <div data-role="footer"> 
     ...</div> 
</div> 
<div id="secondDiv" data-role="page"> 
    <div data-role="content"> 
    </div> 
</div> 
<script type="text/javascript"> 
    (document).ready(function (event) { 
     $('#btnShowCustomers').bind('click', function (event) { 
      GetCustomers(); 
     }); 
    }); 

    function GetCustomers() { 
     var webMethod = "Home/GetCustomers"; 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: webMethod, 
      data: "{}", 
      dataType: "json", 
      success: function (dataObj) { 
       alert('lala'); 
      } 
     }); 
    } 
</script> 

デバッグを取得次のエラー:

document.ready関数 [このエラーのブレーク](ドキュメント).ready(関数(イベント){

ありません3210

これはどのように可能ですか?ドキュメントの準備ができたら、ボタンのクリックイベントのハンドラを登録したいと思います。このは、jQueryのコードは、一般的に、セレクタやその他もろもろのために、そのコンテキストにアクセスするには、この接頭辞を使用して、異なることができますが -

+0

これを '$(document).ready(...')に置き換えてください。そこに '$'変数を入れるのを忘れてしまいました。 – Lapple

+0

jQueryインクルージョンの場合、なぜカミソリを使用しますか?それは外部リソースなので、'によって ' –

+0

あなた'$(document).ready(function(){')の代わりに '$(function(){' – jgauffin

答えて

3
(document).ready(function (event) { 

それが直接readyを呼び出していると考えて、このあなたは$

$(document).ready(function (event) { 
1

は、文字列の先頭に接頭辞$(document).ready(function (event) ...

お知らせドル記号であるべき。

jQueryの利用に関するいくつかの情報については、this pageを参照してください。

1

、する必要がありますdocumentオブジェクトにあります。 readyはjQueryショートカットであり、DOMメソッドではありません。あなたはこのようなJQueryが含まれる場合

1
(document).ready(function (event) { 

あなたは(document)$またはjQueryを持っていない行方不明AR

$(document).ready(function (event) { 
0

:その後、

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.1.min.js" type="text/javascript"></script> 

は、この作業をする必要があり:

$(document).ready(function() { 
    // Handler for .ready() called. 
}); 
0

ショートカットとして、あなたにも書くことができ

を書くことと等価である

$(function(){ 
    //your code 
}); 

$(document).ready(function(){ 
    //your code 
});