2011-08-20 148 views
0

我有一个PHP/JS/MySQL驱动的测验网站有多个选择题。答案选项显示在四个按钮上。禁用onclick,直到JS功能完成

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

有什么办法,当一个按钮被点击后,禁用所有按钮的onclick,直到JS功能完成?就像现在一样,用户可以单击几个选项来解决一个问题,这当然会弄乱结果。

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

非常感谢

回答

4

做到这一点,最简单的方法是有某种标志值:

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; 
+0

这是辉煌的。非常感谢你! – Fred

+0

我从HTML按钮中移动了背景颜色更改脚本,并在JS函数中像这样添加了它,但这似乎阻止了该函数的运行。它有什么问题? if(pending)return; pending = true; //举起旗帜! document.getElementById(answer).style.backgroundColor =“#A9A9A9”; – Fred

0

只要运行在Ajax同步。

在jQuery中,你只需要添加异步= false参数:

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