textbox自动提示

kevin87923 2010-10-09 03:38:53



asp.net 的页面中,有两个文本框用了自动提示。当我两个文本框的自动提示完以后,在将第一个文本的值改变一下。提示框没有显示在第一个文本框下,而是在第二个文本框下,是什么原因?
...全文
167 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin87923 2010-10-09
  • 打赏
  • 举报
回复
嗯。谢谢
孟子E章 2010-10-09
  • 打赏
  • 举报
回复
也可以这样
<%@ Page Language="C#" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body id="b" runat="server">
<form id="form1" runat="server">
<script type="text/javascript">
function Init(txtId) {
d = document.createElement("div");
d.style.position = "absolute";
d.style.display = "none";
d.id = "d" + txtId;
d.style.backgroundColor = "orange";
document.body.appendChild(d);
document.getElementById(txtId).onkeydown = Show;
document.getElementById(txtId).onblur = Hide;
}
function Show(evt) {
ele = window.event ? window.event.srcElement : evt.target;
d = document.getElementById("d" + ele.id);
d.style.display = "";
d.innerHTML = "xxxcccccccccc";
d.style.top = ele.offsetTop + ele.offsetHeight + "px";
d.style.left = ele.offsetLeft + "px";
d.style.width = ele.offsetWidth + "px";

}
function Hide(evt) {
ele = window.event ? window.event.srcElement : evt.target;
d = document.getElementById("d" + ele.id);
d.style.display = "none";
d.innerHTML = "";
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Width="320px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="320px"></asp:TextBox>
</form>
<script type="text/javascript">
window.onload = function () {
Init("<%=TextBox1.ClientID %>");
Init("<%=TextBox2.ClientID %>");
}
</script>
</body>
</html>


或者
<%@ Page Language="C#" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body id="b" runat="server">
<form id="form1" runat="server">
<script type="text/javascript">
function Init(txtId) {
d = document.createElement("div");
d.style.position = "absolute";
d.style.display = "none";
d.id = "d" + txtId;
d.style.backgroundColor = "orange";
document.body.appendChild(d);
document.getElementById(txtId).onkeydown = Show;
document.getElementById(txtId).onblur = Hide;
txt = document.getElementById(txtId);
t = GetXY(txt)
d.style.top = t.y + txt.offsetHeight + "px";
d.style.left = t.x + "px";
d.style.width = txt.offsetWidth + "px";

}
function Show(evt) {
ele = window.event ? window.event.srcElement : evt.target;
d = document.getElementById("d" + ele.id);
d.style.display = "";
d.innerHTML = "xxxcccccccccc";
}
function Hide(evt) {
ele = window.event ? window.event.srcElement : evt.target;
d = document.getElementById("d" + ele.id);
d.style.display = "none";
d.innerHTML = "";
}

function GetXY(txt) {
var r = { x: txt.offsetLeft, y: txt.offsetTop };
while (txt.offsetParent) {
txt = txt.offsetParent;
r.x += txt.offsetLeft;
r.y += txt.offsetTop;
}
return r;

}
</script>
<asp:TextBox ID="TextBox1" runat="server" Width="320px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="320px"></asp:TextBox>
</form>
<script type="text/javascript">
window.onload = function () {
Init("<%=TextBox1.ClientID %>");
Init("<%=TextBox2.ClientID %>");
}
</script>
</body>
</html>
chen_ya_ping 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]

你代码的问题。
[/Quote]
经典
孟子E章 2010-10-09
  • 打赏
  • 举报
回复
你的代码很多。下面是一个简单的实现,获取数据的部分自己可以添加上
<%@ Page Language="C#" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body id="b" runat="server">
<form id="form1" runat="server">
<script type="text/javascript">
function Init(txtId) {
d = document.createElement("div");
d.style.position = "absolute";
d.style.display = "none";
d.id = "d" + txtId;
d.style.backgroundColor = "orange";
document.body.appendChild(d);
}
function HideShow(evt, divId, flag) {
d = document.getElementById("d" + divId);
d.style.display = flag;
d.innerHTML = "xxx";
d.style.top = document.getElementById(divId).offsetTop + document.getElementById(divId).offsetHeight + "px";
d.style.left = document.getElementById(divId).offsetLeft + "px";
d.style.width = document.getElementById(divId).offsetWidth + "px";
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Width="320px" onkeydown="HideShow(event,this.id,'')"
onblur="HideShow(event,this.id,'none')"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="320px" onkeydown="HideShow(event,this.id,'')"
onblur="HideShow(event,this.id,'none')"></asp:TextBox>
</form>
<script type="text/javascript">
Init("<%=TextBox1.ClientID %>");
Init("<%=TextBox2.ClientID %>");
</script>
</body>
</html>
kevin87923 2010-10-09
  • 打赏
  • 举报
回复
前台脚本
<script type ="text/javascript" src ="Lookup.js"></script>
<script type="text/javascript" language="jscript">
mainLoop = function() {
val = queryField.value;
//val = encodeURI(encodeURI(val));
// alert(val);
if (lastVal != val) {
var response = hbkjRDweb.BomInput.GetSearchItems(val);
showQueryDiv(response.value);
lastVal = val;
}
setTimeout('mainLoop()', 100);
return true;
}
</script>
<script type="text/javascript" language="jscript">
mainLoop1 = function() {
val1 = queryField1.value;
//val = encodeURI(encodeURI(val));
// alert(val);
if (lastVal1 != val1) {
var response = hbkjRDweb.BomInput.GetSearchItems(val1);
showQueryDiv1(response.value);
lastVal1 = val1;
}
setTimeout('mainLoop1()', 100);
return true;
}
</script>

<asp:TextBox ID="TextBox1" runat="server" Width="320px"></asp:TextBox>
<script type="text/javascript" language="jscript">
InitQueryCode1("" + '<%= TextBox1.ClientID %>' + "");
</script>

<asp:textbox id="TextBox2" runat="server" Width="320px"></asp:textbox>
<script type="text/javascript" language="jscript">
InitQueryCode("" + '<%= TextBox2.ClientID %>' + "");
</script>
kevin87923 2010-10-09
  • 打赏
  • 举报
回复
Lookup.js



var DIV_BG_COLOR = "#f8d291";
var DIV_HIGHLIGHT_COLOR = "#6699FF";
var DIV_FONT = "Arial";
var DIV_PADDING = "2px";
var DIV_BORDER = "1px solid #CCC";
var queryField1;
var divName1;
var ifName1;
var lastVal1 = "";
var val1 = "";
var globalDiv1;
var card;
var divFormatted1 = false;


function InitQueryCode1(queryFieldName, hiddenDivName) {
queryField1 = document.getElementById(queryFieldName);
queryField1.onblur = hideDiv1;

queryField1.onkeydown = keypressHandler1;
queryField1.autocomplete = "off";

if (hiddenDivName) {
divName1 = hiddenDivName;
}
else {
divName1 = "querydiv";
}

ifName1 = "queryiframe";
setTimeout("mainLoop1()", 100);
}

function getDiv1(divID) {
if (!globalDiv1) {
if (!document.getElementById(divID)) {
var newNode = document.createElement("div");
newNode.setAttribute("id", divID);
document.body.appendChild(newNode);
}
globalDiv1 = document.getElementById(divID);
//计算div左上角的位置
var x = queryField1.offsetLeft;
var y = queryField1.offsetTop + queryField1.offsetHeight;
var parent = queryField1;
while (parent.offsetParent) {
parent = parent.offsetParent;
x += parent.offsetLeft;
y += parent.offsetTop;
}
if (!divFormatted1) {
globalDiv1.style.backgroundColor = DIV_BG_COLOR;
globalDiv1.style.fontFamily = DIV_FONT;
globalDiv1.style.padding = DIV_PADDING;
globalDiv1.style.border = DIV_BORDER;
globalDiv1.style.width = "320px";
globalDiv1.style.fontSize = "90%";
globalDiv1.style.position = "absolute";
globalDiv1.style.left = x + "px";
globalDiv1.style.top = y + "px";
globalDiv1.style.visibility = "hidden";
globalDiv1.style.zIndex = 10000;
divFormatted1 = true;

}
}
return globalDiv1;
}

function showQueryDiv1(resultArray) {
var div = getDiv1(divName1);
while (div.childNodes.length > 0) {
div.removeChild(div.childNodes[0]);
}
for (var i = 0; i < resultArray.length; i++) {
var result = document.createElement("div");
result.style.cursor = "pointer";
result.style.padding = "2px 0px 2px 0px";
result.style.width = div.style.width; //Add width
_unhighlightResult1(result);
result.onmousedown = selectResult1;
result.onmouseover = highlightResult1;
result.onmouseout = unhighlightResult1;

var value = document.createElement("span");
value.className = "value";
value.style.textAlign = "left";
value.style.fontWeight = "bold";
value.innerHTML = resultArray[i];
result.appendChild(value);
div.appendChild(result);
}
showDiv1(resultArray.length > 0);
}

function selectResult1() {
_selectResult1(this);
}
function _selectResult1(item) {
var spans = item.getElementsByTagName("span");
if (spans) {
for (var i = 0; i < spans.length; i++) {
if (spans[i].className == "value") {
queryField1.value = spans[i].innerHTML;
lastVar = val1 = escape(queryField1.value);
mainLoop1();
queryField1.focus();
showDiv1(false);
return;
}
}
}
}

function highlightResult1() {
_highlightResult1(this);
}

function _highlightResult1(item) {
item.style.backgroundColor = DIV_HIGHLIGHT_COLOR;
}

function unhighlightResult1() {
_unhighlightResult1(this);
}

function _unhighlightResult1(item) {
item.style.backgroundColor = DIV_BG_COLOR;
}

function showDiv1(show) {
var div = getDiv1(divName1);
if (show) {
div.style.visibility = "visible";
}
else {
div.style.visibility = "hidden";
}
adjustiFrame1();
}

function hideDiv1() {
showDiv1(false);
}

function keypressHandler1(evt) {
var div = getDiv1(divName1);
if (div.style.visibility == "hidden") {
return true;
}
if (!evt && window.event) {
evt = window.event;
}
var key = evt.keyCode;

var KEYUP = 38;
var KEYDOWN = 40;
var KEYENTER = 13;
var KEYTAB = 9;
if ((key != KEYUP) && (key != KEYDOWN) && (key != KEYENTER) && (key != KEYTAB)) {
return true;
}
var selNum = getSelectedSpanNum1(div);
var selSpan = setSelectedSpan1(div, selNum);
if (key == KEYENTER || key == KEYTAB) {
if (selSpan) {
_selectResult1(selSpan);
}
evt.cancelBubble = true;
return false;
}
else {
if (key == KEYUP) {
selSpan = setSelectedSpan1(div, selNum - 1);
}
if (key == KEYDOWN) {
selSpan = setSelectedSpan1(div, selNum + 1);
}
if (selSpan) {
_highlightResult1(selSpan);
}
}
showDiv1(true);
return true;
}

function getSelectedSpanNum1(div) {
var count = -1;
var spans = div.getElementsByTagName("div");
if (spans) {
for (var i = 0; i < spans.length; i++) {
count++;
if (spans[i].style.backgroundColor != div.style.backgroundColor) {
return count;
}
}
}
return -1;
}
function setSelectedSpan1(div, spanNum) {
var count = -1;
var thisDiv;
var divs = div.getElementsByTagName("div");
if (divs) {
for (var i = 0; i < divs.length; i++) {
if (++count == spanNum) {
_highlightResult1(divs[i]);
thisDiv = divs[i];
}
else {
_unhighlightResult1(divs[i]);
}
}
}
return thisDiv;
}

function adjustiFrame1() {
if (!document.getElementById(ifName1)) {
var newNode = document.createElement("iFrame");
newNode.setAttribute("id", ifName1);
newNode.setAttribute("src", "javascript:false;");
newNode.setAttribute("scrolling", "no");
newNode.setAttribute("frameborder", "0");
document.body.appendChild(newNode);
}
iFrameDiv = document.getElementById(ifName1);
var div = getDiv1(divName1);
try {
iFrameDiv.style.position = "absolute";
iFrameDiv.style.width = div.offsetWidth;
iFrameDiv.style.height = div.offsetHeight;
iFrameDiv.style.top = div.style.top;
iFrameDiv.style.left = div.style.left;
iFrameDiv.style.zIndex = div.style.zIndex - 1;
iFrameDiv.style.visibility = div.style.visibility;
}
catch (e)
{ }
}
勤奋的阿拉丁 2010-10-09
  • 打赏
  • 举报
回复
肯定是代码问题。贴出来看看!
kevin87923 2010-10-09
  • 打赏
  • 举报
回复
但是只用一个的话就可以。
孟子E章 2010-10-09
  • 打赏
  • 举报
回复
你代码的问题。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧