这不容易吗 用 ajax 查询 然后赋值不就OK 了
A 的回车键事件 onkeypress = " query(); "
在
function query(){
if (key == 49)
查数据库。
}
A 的回车键事件 onkeypress = " query(); "
在
function query(){
if (key == 49)
查数据库。
}
调试欢乐多
<textbox id="txtName"/>
function getAjax()
{
var oHttpReq = null;
if(window.ActiveXObject)
oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
else if(window.createRequest)
oHttpReq = window.createRequest();
else
oHttpReq = new XMLHttpRequest();
return oHttpReq;
}
function isEnter(ev)
{
var ev = ev || window.event;
var keycode;
if(window.event) keycode = ev.keyCode;
else keycode = ev.which;
if(keycode == 10 || keycode == 13) return true;
else return false;
}
function getName(ev)
{
var ev = ev || window.event;
if(!isEnter(ev)) return;
var req = getAjax();
req.open("GET", "test.aspx?code="+document.getElementById("txtCode").value, true);
req.onreadystatechange = function(){
if(req.readyState==4 || req.readyState=="complete"){
var re = req.responseText;//test.aspx页面上多有点内容
document.getElementById("txtName").value = re;
document.getElementById("txtName").focus();
}
};
req.send(null);
}
test.aspx.cs
page_load()
{
string name = DB.GetValue("select name from a where code='"+Request["code"]+"'");
Response.Write(name);//req.responseText
Response.End();//必要的
}
细节自己处理
//创建xmlRequest
function createXMLHttp()
{
if (window.XMLHttpRequest) {
var xmlObj = new XMLHttpRequest();
}
else {
var MSXML = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for (var n = 0; n < MSXML.length; n++) {
try {
var xmlObj = new ActiveXObject(MSXML[n]);
break;
}
catch(e) {
}
}
}
return xmlObj;
}
//根据 url 获取 xml数据字符串
function getDataByXmlHttp(url)
{
var xmlhttp;
var s = null;
xmlhttp = createXMLHttp();
if (null != xmlhttp)
{
xmlhttp.open("get", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
s = xmlhttp.responseText;
}
return s;
}
function query(){
if (key == 49)
document.getElementById("B").value = getDataByXmlHttp(url) ;
}url 为你请求的(查数据的)路径 + 参数