如何根据ajax响应禁用带有ajax的提交按钮

时间:2009-09-05 12:32:45

标签: javascript ajax

我试图在我的代码中使用这样的东西,但没有运气......

function captcha(data) {
    var httpxml;
    try {
        // Firefox, Opera 8.0+, Safari
        httpxml = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            httpxml = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpxml = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    function stateck() {
        if (httpxml.readyState == 4) {
            document.getElementById("cap_error").innerHTML = httpxml.responseText;
            var result = httpxml.responseText;
            if (result == 'Error..try again') {
                document.getElementById("regSubmit").disabled = true;
            }


        }
    }
    var url = "includes/captcha.php";
    url = url + "?datacap=" + data;
    url = url + "&sid=" + Math.random();
    httpxml.onreadystatechange = stateck;
    httpxml.open("GET", url, true);
    httpxml.send(null);
}

编辑:

我的提交按钮是:

<input type="submit"  value="Submit" id="regSubmit" name="Submit"/>  

当ajax函数返回'Error..try'时,应禁用regSubmit按钮。但是这个功能现在不适用于我。

if (result == 'Error..try again')
{
  document.getElementById("regSubmit").disabled = true;
}

1 个答案:

答案 0 :(得分:1)

这就是我们在我们的应用中所拥有的:

document.getElementById('buttonId').disabled=true;

它有效。

两件事:

1 - 您应该在更改javascript后清除浏览器缓存。

2 - 在禁用按钮之前发出警报,以确保您进入“if”语句。