AJAX将PHP表单元素返回到原始CSS样式

时间:2012-02-16 17:05:46

标签: ajax

我在PHP页面上使用以下Ajax代码来检查是否填充了销售税字段。仅在输入有效的邮政编码时才会填充销售税字段。如果将css样式标记为缺少值,该如何更改css样式?

var xmlhttp;


function showUser(str) {

    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="response.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function stateChanged() {
    if (xmlhttp.readyState==4) {
        document.getElementById("result").value=xmlhttp.responseText;
        if(document.getElementById("result").value =='') {
            validateZip();
        }
    }
}

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function validateZip() {
    if(document.newagreementform.tax.value=="") {
        alert("A valid zip code in our service area is required.");
        document.newagreementform.zipcode.focus();
        document.newagreementform.zipcode.style.backgroundColor="#ffff99";
        document.newagreementform.zipcode.style.borderColor="#990000";      
        return false;
    }
    if(document.newagreementform.tax.value!=""){document.newagreementform.zipcode.style.backgroundColor="#ffffff";} 
}

1 个答案:

答案 0 :(得分:0)

如何将事件放在邮政编码的输入字段上......如:

<input Id="Zip" onkeypress="RemoveZipCodeErrorMessage()">

然后你的RemoveZipCodeErrorMessage()函数看起来像:

function RemoveZipCodeErrorMessage()
{
// change to color you want to return to
document.newagreementform.zipcode.style.backgroundColor="#ffff99";                          
document.newagreementform.zipcode.style.borderColor="#990000"; 
}

安德鲁