开发者

jQuery $.get refreshing page instead of providing data

开发者 https://www.devze.com 2022-12-16 17:56 出处:网络
I have written some code using jQuery to use Ajax to get data from another WebForm, and 开发者_高级运维it works fine.I\'m copying the code to another project, but it won\'t work properly.When a class

I have written some code using jQuery to use Ajax to get data from another WebForm, and 开发者_高级运维it works fine. I'm copying the code to another project, but it won't work properly. When a class member is clicked, it will give me the ProductID that I have concatenated onto the input ID, but it never alerts the data from the $.get. The test page (/Products/Ajax/Default.aspx) that I have set up simply returns the text "TESTING...". I installed Web Development Helper in IE, and it shows that the request is getting to the test page and that the status is 200 with my correct return text. However, jQuery refreshes my calling page before it will ever show me the data that I'm asking for. Below are the code snippets from my page. Please let me know if there are other code blocks that you need to see. Thank you!

 <script type="text/javascript">
        $(document).ready(function() {
            $(".addtocart_a").click(function() {

                var sProdIDFileID = $(this).attr("id");
                var aProdIDFileID = sProdIDFileID.split("_");
                var sProdID = aProdIDFileID[5];
                // *** This alert shows fine -- ProdID: 7
                alert("ProdID: " + sProdID);

                $.get("/Products/Ajax/Default.aspx", { test: "yes" }, function(data) {
                    // *** This alert never gets displayed
                    alert("Data Loaded: " + data);
                }, "text");
            });
        });    
    </script>

<input src="/images/add_to_cart.png" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$aAddToCart_7" type="image" id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_aAddToCart_7" class="addtocart_a" />


The easiest way is to tell jQuery not to return anything.

$(".addtocart_a").click(function(e){

  // REST OF FUNCTION

  return false;
});

Good luck! If you need anything else let me know.

0

精彩评论

暂无评论...
验证码 换一张
取 消