1、原生态AJAX
<form> First Name:<input type="text"id="txt1"
onkeyup="showHint(this.value)"
/> </form> <p>Suggestions: <spanid="txtHint"
></span></p>
function showHint(str) { if (str.length==0
) { document.getElementById("txtHint").innerHTML=""; return; } xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("您的浏览器不支持AJAX!"); return; } varurl
="gethint.asp"; url=url+"?q
="+str; url=url+"&sid
="+Math.random(); xmlHttp.onreadystatechange=stateChanged
; xmlHttp.open("GET",url,true); xmlHttp.send(null); }
stateChanged() 函数包含下面的代码:
function stateChanged() { if (xmlHttp.readyState==4) {2基于easyUI,jquery校验用户名是否可用的AJAXdocument.getElementById("txtHint").innerHTML=xmlHttp.responseText
; } }
<span style=
"font-family: consolas;"
face=
"Consolas"
> <script type=
"text/javascript"
language=
"javascript"
>
function
checkName() {
$.ajax({
type:
"GET"
,
url:
"CheckName.aspx"
,
dataType:
"html"
,
data:
"name="
+ $(
"#UserAccount"
).val(),
beforeSend:
function
(XMLHttpRequest) {
$(
"#ErrorOrRight"
).text(
"正在查询....."
);
},
success:
function
(msg) {
$(
"#ErrorOrRight"
).html(msg);
$(
"#ErrorOrRight"
).css(
"color"
,
"Green"
);
},
complete:
function
(XMLHttpRequest, textStatus) {
//隐藏正在查询图片
},
error:
function
() {
//错误处理
}
});
}
</script></span>
<input class="textinput" id="UserAccount" name="UserAccount" sizset="1" runat="server" οnblur="checkName()">