简体   繁体   English

第二次单击后按钮不起作用

[英]Button Doesn't Work After Second Click

I have a button that calls a function in javascript. 我有一个调用javascript函数的按钮。 The javascript in turn runs two consecutive ajax calls. JavaScript依次运行两个连续的Ajax调用。 After the first one finishes, it does some extra work, then runs the second ajax call. 第一个完成后,它会做一些额外的工作,然后运行第二个ajax调用。

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. 它是从按钮中被称为“ onclick”的功能。

I am pretty new to ajax but I'm sure that this shouldn't be happening. 我对ajax相当陌生,但是我敢肯定这不会发生。 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... 如果您有兴趣,这是我的ajax电话...

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 DON'T 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. 所以我在save_session()函数内部进行了jQuery追加操作。 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. 我的猜测是,通过将输入“ save_session”命名为与内存中的save_session()函数定义混淆。 Now there wasn't a definition conflict, then it was okay. 现在没有定义冲突,那就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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