2012-11-11 66 views
1

我想知道爲什麼這個hander點擊兩次按鈕?爲什麼這個處理程序在兩次按鈕點擊都會觸發?

<script type="text/javascript"> 
    $(document).ready("#testbutton").click(function() { 
     $.ajax({ 
      url: '@Url.Action("RenderBlogComments","Home")', 

      success: function (data) { 
       // your data could be a View or Json or what ever you returned in your action method 
       // parse your data here 
       alert(data); 
      } 
     }); 
    }); 
</script> 

<input type="submit" name="testbutton" value="submit" id="testbutton" /> 

<input type="submit" name="testbutton" value="submit" id="testbutton2" /> 
+3

$(document).ready(「#testbutton」)'沒有任何意義並返回文檔。 – SLaks

回答

1

通過這一解決方案,只有第一個按鈕觸發Ajax調用。

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="content-type" content="text/html; charset=utf8" /> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script> 
     <script type="text/javascript"> 
      $(document).ready(function(){ 
       $("#testbutton").click(function() { 
        $.ajax({ 
         url: 'http://www.google.de', 
         success: function (data) { 
          // your data could be a View or Json or what ever you returned in your action method 
          // parse your data here 
          alert(data); 
         } 
        }); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <input type="submit" name="testbutton" value="submit" id="testbutton" /> 
     <input type="submit" name="testbutton" value="submit" id="testbutton2" /> 
    </body> 
</html> 
2

嘗試

$(document).ready(function(){ 

     $(""#testbutton"").click(function() { 
     $.ajax({ 
      url: '@Url.Action("RenderBlogComments","Home")', 

      success: function (data) { 
       // your data could be a View or Json or what ever you returned in your action method 
       // parse your data here 
       alert(data); 
      } 
     }); 
    }); 
}); 

相關問題