简体   繁体   中英

Button Doesn't Work After Second Click

I have a button that calls a function in javascript. The javascript in turn runs two consecutive ajax calls. After the first one finishes, it does some extra work, then runs the second ajax call.

The button works upon first clicking it. However, when I want to click it again the following error pops up...

Uncaught TypeError: object is not a function

It is in reference to my function that is being called 'onclick' from the button.

I am pretty new to ajax but I'm sure that this shouldn't be happening. Other buttons are working just fine, and they all call functions from the same script. It just seems to be this one function. I would have expected there to be a semicolon missing or something, but then the first time wouldn't have worked... Also, I do know that the function finished executing, since I debugged the function and it reaches the bottom...

Here are my ajax calls in case you're interested...

var $response = $.ajax({
    url: $abs_filename,
    type: 'HEAD',
    async: false,
    success: function () {
        console.log('done');
    }
}).status;
($response != "200") ? $exist = false : $exist = true;

....lots of extra code here ....

var response = $.ajax({
    type: 'POST',
    url: '/SERT/includes/file_operations.php',//url of receiver file on server
    data: {saves: $save_data}, //your data
    dataType: 'text', //text...
    success: function(res) {
        alert(res);
    },
    async: false
}).status;

EDIT: My function is called by

<input type="button" .... onclick="save_session()">

You've not actually shown the code that is causing the error.

However...

Don't do this; it doesn't do what you think it does.

($response != "200") ? $exist = false : $exist = true;

Do this:

$exist = $response == "200";

And just use synchronous XHR. 使用同步XHR。

I was able to figure it out...

So I had a jQuery append operation going on inside of the save_session() function. This operation was as such...

$bottom_section.append('<input type="hidden" name="save_session" value="' + $total + ' ' + $paragraph + '">');

When I took this out then the whole thing worked as expected. My guess is that by naming the input "save_session" messed with the function definition of save_session() in memory. Now there wasn't a definition conflict, then it was okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM