我有一张表select id,name from aa 还有一张页面。此页面使用了母板页 页面上有两个textbox1 textbox2 我想根据textbox1输入的内容,当textbox1失去焦点后,从数据库中读取相对应的name值 放进textbox2中 要求不刷新页面,要局部刷新,因为页面中还有其他东西
test.html: <input id=txt1 onblur="getpara(this);"/><input id=txt2 /> <script> function getpara(obj){ if(obj.value.length==0){return;}//Nullの場合、処理終了 var id=obj.value; var xmlHttp=createXMLHttpRequest(); url='photo3.aspx?id='+id+'&s='+ new Date().getTime(); xmlHttp.open('GET',url,true); xmlHttp.onreadystatechange=function(){homel(xmlHttp)}; xmlHttp.send(null); //debugger; }function homel(x){ if (x.readyState==4){ if(x.status==200||x.status==0){ document.getElementById("txt2").value=x.responseText; } } } function createXMLHttpRequest() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { return null; } } } else { return null; } }</script> photo3.aspx: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% dim Session dim db dim rs dim sql Dim id
id = Request.QueryString("id") Session = Server.CreateObject("OracleInProcServer.XOraSession") db = Session.DbOpenDatabase("database","uid/psd",0) sql="Select name From aa where id=" & id rs = db.CreateDynaset(sql,0)
Response.ContentType = "text/xml"
If Not rs.Eof Then Response.Write(rs("name").value) End If
而后用Jquery Ajax作局部刷新
用Jquery
$('#ctl00_ContentPlaceHolder1_textbox1').blur(function(){
$.ajax({
type: 'POST',
data: {
key: escape(key)
},
dataType: 'json',
url: 'json.ashx',
beforeSend: function(XMLHttpRequest){
$('#ctl00_ContentPlaceHolder1_textbox2').val('加载中');
},
success: function(data){
$('#ctl00_ContentPlaceHolder1_textbox2').val(data.name);
},
complete: function(XMLHttpRequest){ }
})
})
<input id=txt1 onblur="getpara(this);"/><input id=txt2 />
<script>
function getpara(obj){
if(obj.value.length==0){return;}//Nullの場合、処理終了
var id=obj.value;
var xmlHttp=createXMLHttpRequest();
url='photo3.aspx?id='+id+'&s='+ new Date().getTime();
xmlHttp.open('GET',url,true);
xmlHttp.onreadystatechange=function(){homel(xmlHttp)};
xmlHttp.send(null);
//debugger;
}function homel(x){
if (x.readyState==4){
if(x.status==200||x.status==0){
document.getElementById("txt2").value=x.responseText;
}
}
}
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
return null;
}
}
} else {
return null;
}
}</script>
photo3.aspx:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<% dim Session
dim db
dim rs
dim sql
Dim id
id = Request.QueryString("id")
Session = Server.CreateObject("OracleInProcServer.XOraSession")
db = Session.DbOpenDatabase("database","uid/psd",0)
sql="Select name From aa where id=" & id
rs = db.CreateDynaset(sql,0)
Response.ContentType = "text/xml"
If Not rs.Eof Then
Response.Write(rs("name").value)
End If
rs.Close
db.close
db=nothing
Session = Nothing
Response.End()
%>