开发者

jQuery stopping button postback

开发者 https://www.devze.com 2023-04-12 23:20 出处:网络
I need some help with jQuery and stopping an asp.net button postback.If you look at the code below, I defined a variable named err and gave it a value of 0.Then I have jquery geta string from a web se

I need some help with jQuery and stopping an asp.net button postback. If you look at the code below, I defined a variable named err and gave it a value of 0. Then I have jquery get a string from a web service, it's actually an error string. If the length of this string is 0, then set err equals to 1, otherwise set err equals to 2 because we're getting back a business rule error.

When I debug the app, it seems that err equals 0, and never 1 or 2. It also seems that the web service gets its value after my last if statement where i'm supposed to stop the postback.

Any help would be appreciated. Thanks.

   $(document).ready(function () {
      $('#<%=Button1.ClientID %>').click(function (e) {

        //Find out the hour and time of the current request
        var myString = $('#<%=DropDownList1.ClientID %>').val();
        var myResult = myString.split(":");
        var myHour = myResult[0];
        var myTime = myResult[1];
        var xDate = $("*[id$='hiddenDate']").val();
        var err = 0;
        //Check to see if the end time of the event somehow falls during a time when another event is occuring.
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "StudiesWebService.asmx/CheckEndTime",
            data: "{'UserName':'" + "jacksonm" + "','xDate':'" + xDate + "','xHour':'" + myHour + "','xMinutes':'" + myTime + "'}",
            dataType: "json",
            success: function (data) {
                var myReturnedString = data.d;
                //if the returned string length is greater than 0, then set err = 2
                if (myReturnedString.length = 0) {
                    err = 1;
                }
                else {
                    err = 2; 

                }

                $('.result').html(myReturnedString);
            },
            error: function (e) {
                $('.result').html("An Error occured checking the validity of this event." + e);
                err = 2;

            }



        });

         //stop the postback here depending on what err equals. 
        if (err == 2) {
            return false;
        }

        else {
            return true;

        }



    });

   });

<asp:Button ID="Button1"开发者_如何学运维 runat="server" Text="Submit Time" class="CLOSE" onclick="Button1_Click"   />


Unless your Ajax web service call is actually synchronous then the order your code is executing is not as you expect. Add an alert to your success callback, your error callback, and right before this comment //stop the postback here depending on what err equals. The last alert should happen first, since the web service call won't return and be executed until after the current function completes.

In your Ajax call, add this option async: false:

$.ajax({
        type: "POST",
        async: false,


You're setting your myReturnedString.length var to 0!

try changing the line

if (myReturnedString.length = 0) {...}

to

if (myReturnedString.length == 0) {...}

EDIT: to stop the postback you can try:

    error: function (e) {
        $('.result').html("An Error occured checking the validity of this event." + e);
        err = 2;
        return false;
    }
0

精彩评论

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