簡體   English   中英

禁用onclick,直到完成JS功能

[英]Disable onclick until JS function is done

我有一個PHP / JS / MySQL驅動的測驗網站,上面有多項選擇題。 答案選項顯示在四個按鈕上。

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

有沒有辦法在單擊按鈕后禁用所有按鈕的onclick功能,直到JS功能完成? 現在,用戶可以單擊一個問題的多個替代方案,這當然會弄亂結果。

由onclick調用的JS函數使用AJAX調用一個PHP文件來處理答案。

非常感謝

最簡單的方法是具有某種標志值:

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;

只需同步運行Ajax。

在jQuery中,您只需要添加async = false參數:

$.ajax({
    url:'foo.handler.php',
    data:params,
    async:false,
    success:function(){
        // do something cool.
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM