Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.Net app uses JQuery AJAX. Before the AJAX call I can update the input in a text box, but after the AJAX call it seems completely disabled allowing no editing in text box, but other controls (CheckBox, Buttons, datepicker) are still enabled.
Notice at the end in .complete, I added a line to try to force it enabled, no help though:
$('#idtxtLoggingDaysDisplayed').prop('disabled', false);
Any suggestions would be very appreciated. Thanks, Mike


$.ajax({
    type: "POST",
    url: "GC_UserErrors.aspx/receiveAJAXMessage",
    data: sMsg1,
    beforeSend: function () {
        $(document).keydown(function () { return false; }); // disable keyboard input
        $(".overlay").show(); // show loader
        $('#loadingGIF').show();
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: function () {
        $(document).keydown(function () { return true; }); // enable keyboard
        $(".overlay").hide(); //hide loader
        $('#loadingGIF').hide();
        $('#idtxtLoggingDaysDisplayed').prop('disabled', false);
    },
    success: OnSuccess,
    failure: //function (response) {
        OnFailure
});


What I have tried:

Also tried
$('#idtxtLoggingDaysDisplayed').attr('disabled', false);
Posted
Updated 25-Oct-18 8:35am
Comments
MadMyche 24-Oct-18 11:42am    
Any errors being thrown/shown in the browsers developer console? (eg firebug)
Michael Breeden 24-Oct-18 12:34pm    
No, there's nothing in the console. (Using Chrome Developer Tools.) ... That was a good call. I tested in FF and there is nothing in the console but what I put there.
MadMyche 24-Oct-18 13:04pm    
And is there data being returned?
Michael Breeden 24-Oct-18 13:09pm    
Yes. I'm reading the value in the textbox and sending it with AJAX. It works the first time, then the text box becomes "read only" or "disabled", but I can update other controls and re-run the AJAX process. All data is read and sent correctly including the data in the text box. I just cannot modify that data.
Bryian Tan 24-Oct-18 21:44pm    
What version of jQuery the code is using? complete() function could be deprecated . replaces with always() jQuery.ajax() | jQuery API Documentation[^]

1 solution

Quote:
comment out the code:
$(document).keydown(function () { return false; });

That fixes the problem, though it does not explain it...

The problem is, you're not removing the event listener; you're simply adding another event listener.

You now have two listeners for the same event. One returns false; the other returns true. There's no guarantee which order these listeners will be executed in, and no way of combining the return value from both listeners.

Move your listener to a separate function. Then you can use the off method[^] to remove the listener.

You can also move the beforeSend code immediately before the ajax call, and use the promise interface methods for the callbacks.
JavaScript
var preventKeyboard = function() { return false; };

$(document).keydown(preventKeyboard); // disable keyboard input
$(".overlay").show(); // show loader
$('#loadingGIF').show();

$.ajax({
    type: "POST",
    url: "GC_UserErrors.aspx/receiveAJAXMessage",
    data: sMsg1,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
})
.done(OnSuccess)
.fail(OnFailure)
.always(function(){
    $(document).off("keydown", preventKeyboard); // enable keyboard
    $(".overlay").hide(); //hide loader
    $('#loadingGIF').hide();
    $('#idtxtLoggingDaysDisplayed').prop('disabled', false);
});
 
Share this answer
 
Comments
Michael Breeden 25-Oct-18 13:54pm    
Awesome Richard... Obvious in hindsight, but a simple mistake. Thank you much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900