1

我想知道为什么这个处理程序会在两次按钮点击时触发?

<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" />
4

2 回答 2

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);
            }
        });
   });
});

于 2012-11-11T20:00:11.780 回答
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>
于 2012-11-11T20:22:36.627 回答