禁用onclick直到JS功能完成

Fre*_*red 0 javascript ajax onclick

我有一个PHP/JS/MySQL驱动的测验站点,有多个选择题.答案替代方案显示在四个按钮上.

<input type = "button" value="$alt1" onClick="changeQuestion('alternative1'); this.style.backgroundColor = '#A9A9A9'">
Run Code Online (Sandbox Code Playgroud)

是否有任何方法,当单击一个按钮时,禁用所有按钮的onclick直到JS功能完成?就像现在一样,用户可以点击一个问题的几个替代方案,这当然会弄乱结果.

onclick调用的JS函数使用AJAX来调用处理答案的PHP文件.

非常感谢

cwa*_*ole 5

最简单的方法是使用某种标志值:

var pending = false;

function changeQuestion(nam)
{
    // test to see if something else set the state to pending.
    // if so... return, we don't want this to happen.
    if(pending) return;
    pending = true; // raise the flag!

    /* ... later ... */
    // in the success method of your AJAX call add:
    pending = false;
Run Code Online (Sandbox Code Playgroud)