有關于getElementById("服務器控件id") 应该用var key = document.getElementById("<%=KProvince.ClientID%>").value 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自己可定义个trim方法,就像调用系统其他的字符串函数一样方法是:在js中的最上面加入String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}然后在下面的代码就可以这样用var s=" str ";alert(s.trim());//返回"str" 錯誤 1 名稱 'KProvince' 不存在於目前內容中 D:\SearchTip\Default.aspx 85 将你变量key,换个名字试试如果不行,把方法写成这样:function beKeyUp(bku){ alert(bku.value); return; if(bku.value.length>0) { xmlHttpInitializtions(); xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(bku.value),true); xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttpRequest.onreadystatechange=stateChange; xmlhttpRequest.Send(); } }调用那边写:onkeyup="beKeyUp(this);" 你是不是在这页面上有两个或以上叫KProvince的控件? 只有一個KProvince,全在一個文件中 那你就把控件传进去好了,多个参数<input id="KProvince" autocomplete="off" onkeyup="beKeyUp(this);" value=' <%# DataBinder.Eval(Container,"Dataitem.SUPPLIER")%>' type="text" runat="server"/> function beKeyUp(obj) { var key = obj.value;//得不到KProvince控件,請問是怎么回事? alert(key); return; if(key.length>0)//有值才POST,这里最好加个Trim()方法,但是JAVASCRIPT没有现成的方法,自己定义。 { xmlHttpInitializtions(); xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(key),true);//POST xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttpRequest.onreadystatechange=stateChange;//返回状态调用方法stateChange xmlhttpRequest.Send(); } } 有可能是你的id="KProvince" 在程序生成HTML的时候已换成其他的名字了,请查看客户端html找到ID,然后再getElementById 如果不行的话你就加一个参数比如:onkeyup="beKeyUp(this.id)function beKeyUp(id) { var key = document.getElementById(id).value;//看看这样是否可以得到 还有你这个服务器控件是属于什么的?JSP?ASP.NET?还是? var key = document.getElementById("KProvince").value;//得不到KProvince控件,請問是怎么回事?alert(key); 你说得不到,有什么样的提示?弹出什么样的对话框 腳本錯誤,此處需要物件asp.net控件 .ENT的,你就看下这个http://www.cnblogs.com/zhujingyuan/archive/2007/11/23/969361.html吧看完你就明白了 http://www.cnblogs.com/ivanyb/archive/2008/03/28/1127473.html还有这个 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><html><head runat="server"> <style type="text/css"> body,td{ font-size:12px; color:#333333; } .item_normal { BORDER-LEFT: #666 1px solid; BORDER-RIGHT: #666 1px solid; width:200px; background-color:#ffffff; overflow:hidden; } .itemBg { BORDER-LEFT: #666 1px solid; BORDER-RIGHT: #666 1px solid; cursor:default; background-color:#ffffff; width:200px; } .item_high { background-color:#326BC5; cursor:default; color:white; } .item_button { BORDER-LEFT: #666 1px solid; BORDER-RIGHT: #666 1px solid; BORDER-BOTTOM: #666 1px solid; text-align:right; background-color:#ffffff; width:200px; cursor:hand; } .suggest_hidden { display:none; } </style> <script language="javascript" type="text/javascript" charset="utf-8"> var array=new Array(); var zz=-1; function xmlHttpInitializtions() { try { xmlhttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlhttpRequest = false; } } if (!xmlhttpRequest && typeof XMLHttpRequest != 'undefined') { xmlhttpRequest = new XMLHttpRequest(); } } function beKeyUp() { var key = document.getElementById("KProvince").value;//錯誤信息:此處需要物件 if(key.length>0) { xmlHttpInitializtions(); xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(key),true);//POST xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttpRequest.onreadystatechange=stateChange;//返回状态调用方法stateChange xmlhttpRequest.Send(); } } function stateChange() { if(xmlhttpRequest.readystate==4) { if(xmlhttpRequest.status==200) { var responseStr=xmlhttpRequest.responseText;//获取返回值 if(responseStr.length>0)//返回值不为空才执行下面的代码 { array=responseStr.split('|');//根据‘|’拆分,根据自己任意特殊字符,初始化数组 positionDiv();//调用方法,定位DIV显示,具体见方法解释 document.getElementById("search_suggest").innerHTML="";//每次清空DIV内容 for(var i=0;i<array.length;i++) { if(array[i]!="")//项值不为空,组合DIV,每个DIV有onmouseover、onmouseout、onclick对应事件 { document.getElementById("search_suggest").innerHTML+="<div id='item" + i + "' class='itemBg' onmouseover='beMouseOver(" + i +")' onmouseout='beMouseOut(" + i + ")' onclick='beClick(" + i + ")'>" + array[i] + "</div>"; } } //最后一个DIV显示 关闭 效果 onclick方法 document.getElementById("search_suggest").innerHTML+="<div class='item_button' onclick='hiddenDiv();'><font color='#999999'>关闭</font></div>"; document.getElementById("search_suggest").style.display="inline"; } else { document.getElementById("search_suggest").style.display="none"; } } } } //定位DIV显示 function positionDiv() { var DivRef= document.getElementById("search_suggest"); DivRef.style.position = "absolute"; var t=document.getElementById("KProvince");//錯誤信息:此處需要物件 DivRef.style.top= getAbsolutePos(t).y + 20;//相对文本框的TOP高度,方法见下面 DivRef.style.left= getAbsolutePos(t).x ;//相对文本框的LEFT宽度 DivRef.style.height=array.length * 20;//DIV的高度等于每行20个象素乘行数(也就是数组的长度,体现全局数组的作用,不然要传入数组长度的参数) } //实现最后一个DIV 关闭 onclick方法 function hiddenDiv() { document.getElementById("search_suggest").style.display="none"; } //定位方法,不做解释 function getAbsolutePos(el) { var SL = 0, ST = 0; var is_div = /^div$/i.test(el.tagName); if (is_div && el.scrollLeft) SL = el.scrollLeft; if (is_div && el.scrollTop) ST = el.scrollTop; var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST }; if (el.offsetParent) { var tmp = this.getAbsolutePos(el.offsetParent); r.x += tmp.x; r.y += tmp.y; } return r; } //最后是鼠标效果的方法。 //函数鼠标经过效果 function beMouseOverEFF(i) { if ((i>=0)&(i<=array.length-1)) { document.getElementById("item" + i).className="item_high"; } } //函数鼠标移开效果 function beMouseOutEFF(i) { if ((i>=0)&(i<=array.length-1)) { document.getElementById("item" + i).className="item_normal"; } } //函数鼠标经过 function beMouseOver(i) { document.getElementById("KProvince").focus(); beMouseOutEFF(zz); zz=i; beMouseOverEFF(zz); } //函数鼠标移开 function beMouseOut(i) { beMouseOutEFF(i); } //函数单击 function beClick(i) { document.getElementById("KProvince").value=array[i]; document.getElementById("search_suggest").style.display="none"; document.getElementById("KProvince").focus(); } </script></head><body> <form id="form1" runat="server"> <h3> 目前版本还不支持上下键的选择功能,还不能遮挡选择控件,下周版本将支持这一功能</h3> <br /> <!--<input id="KProvince" autocomplete="off" onkeyup="beKeyUp();" style="position: absolute; width: 200px; border: #666 1px solid;" />--> <div id="search_suggest" style="position: absolute; background-color: #fff; z-index: 1;"> </div> <br /><br /><br /> Email:[email protected] <asp:DataGrid ID="myDataGrid" runat="server" AutoGenerateColumns="False" OnCancelCommand="myDataGrid_CancelCommand" OnEditCommand="myDataGrid_EditCommand" Width="443px" OnUpdateCommand="myDataGrid_UpdateCommand"> <Columns> <asp:BoundColumn DataField="id" HeaderText="編號" ReadOnly="True"> <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> </asp:BoundColumn> <asp:TemplateColumn HeaderText="名字"> <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> <ItemTemplate> <asp:Label id="lbl_Name" runat="server" Text='<%# DataBinder.Eval(Container,"Dataitem.name")%>'></asp:Label> </ItemTemplate> <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> <EditItemTemplate> <asp:TextBox ID="KProvince" AutoCompleteType="None" onkeyup="beKeyUp();" Text='<%# DataBinder.Eval(Container,"Dataitem.name")%>' runat="server"></asp:TextBox> </EditItemTemplate> </asp:TemplateColumn> <asp:EditCommandColumn CancelText="取消" EditText="編輯" HeaderText="編輯" UpdateText="更新"> <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" Wrap="False" /> </asp:EditCommandColumn> </Columns> </asp:DataGrid> </form></body></html>以上就是我的頁面代碼,紅色標記的地方就是得不到控件id 看下生成的html里的id再用document.getElementById就可以了 js是操作客户端的脚本..而服务器端的控件会被服务器进行编译后,再输出HTML, 就是说.. 服务器控件的ID有可能不是HTML控件中的ID. KProvince.ClientID 才是真正HTML代码中的控件ID. var key = document.getElementById("DataGrid_PO_ctl02_Label400").value;我已經把id改成html代碼的id了 alert(key); return;為什么key 里面的值會是undefined 源码里显示的DataGrid_PO_ctl02_Label400是什么控件? 如果是<INPUT type="text"/> 那么就是value属性.如果不是,那么应该是innerText属性..你可以尝试将二者都alert出来..试试看. 应该不是找不到KProvince,是找不到它的value吧,我觉得 判断当前的用户是在用PC浏览器还是手机浏览器还是平板电脑浏览器 为什么jquery不能添加子元素 有关一个js 操作 css 的很诡异的问题~~真的很诡异啊 高手们帮看一下这个JS里面调用是什么意思,本人是菜鸟 如何通过javascript获得当前月的日子总数? innerHTML JS如何定位当前元素在DOM结构中的位置 用ajax 怎么抓取登录的QQ 号码 怎么在提交时判断:必须选中每个radio 紧急求助!!!怎样动态添加select中的列表值? Firefox 在线编辑器按回车键,就是加个<br>而不是加<p></p>,帮忙解决好吗? 分享,值为空时表格边框不显示,其实显示出来很容易
方法是:在js中的最上面加入String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}然后在下面的代码就可以这样用
var s=" str ";
alert(s.trim());//返回"str"
錯誤 1 名稱 'KProvince' 不存在於目前內容中 D:\SearchTip\Default.aspx 85
function beKeyUp(bku)
{
alert(bku.value);
return;
if(bku.value.length>0)
{
xmlHttpInitializtions();
xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(bku.value),true);
xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttpRequest.onreadystatechange=stateChange;
xmlhttpRequest.Send();
}
}
调用那边写:
onkeyup="beKeyUp(this);"
<input id="KProvince" autocomplete="off" onkeyup="beKeyUp(this);" value=' <%# DataBinder.Eval(Container,"Dataitem.SUPPLIER")%>' type="text" runat="server"/> function beKeyUp(obj)
{
var key = obj.value;//得不到KProvince控件,請問是怎么回事?
alert(key);
return;
if(key.length>0)//有值才POST,这里最好加个Trim()方法,但是JAVASCRIPT没有现成的方法,自己定义。
{
xmlHttpInitializtions();
xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(key),true);//POST
xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttpRequest.onreadystatechange=stateChange;//返回状态调用方法stateChange
xmlhttpRequest.Send();
}
}
比如:onkeyup="beKeyUp(this.id)
function beKeyUp(id)
{
var key = document.getElementById(id).value;//
看看这样是否可以得到
JSP?ASP.NET?还是?
alert(key);
你说得不到,有什么样的提示?弹出什么样的对话框
asp.net控件
看完你就明白了
还有这个
<html>
<head runat="server">
<style type="text/css">
body,td{
font-size:12px;
color:#333333;
}
.item_normal
{
BORDER-LEFT: #666 1px solid;
BORDER-RIGHT: #666 1px solid;
width:200px;
background-color:#ffffff;
overflow:hidden;
}
.itemBg
{
BORDER-LEFT: #666 1px solid;
BORDER-RIGHT: #666 1px solid;
cursor:default;
background-color:#ffffff;
width:200px;
}
.item_high
{
background-color:#326BC5;
cursor:default;
color:white;
}
.item_button
{
BORDER-LEFT: #666 1px solid;
BORDER-RIGHT: #666 1px solid;
BORDER-BOTTOM: #666 1px solid;
text-align:right;
background-color:#ffffff;
width:200px;
cursor:hand;
}
.suggest_hidden
{
display:none;
} </style>
<script language="javascript" type="text/javascript" charset="utf-8">
var array=new Array();
var zz=-1;
function xmlHttpInitializtions()
{
try
{
xmlhttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
xmlhttpRequest = false;
}
}
if (!xmlhttpRequest && typeof XMLHttpRequest != 'undefined')
{
xmlhttpRequest = new XMLHttpRequest();
}
}
function beKeyUp()
{
var key = document.getElementById("KProvince").value;//錯誤信息:此處需要物件
if(key.length>0)
{
xmlHttpInitializtions();
xmlhttpRequest.Open("POST","Result.aspx?key=" + encodeURI(key),true);//POST
xmlhttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttpRequest.onreadystatechange=stateChange;//返回状态调用方法stateChange
xmlhttpRequest.Send();
}
}
function stateChange()
{
if(xmlhttpRequest.readystate==4)
{
if(xmlhttpRequest.status==200)
{
var responseStr=xmlhttpRequest.responseText;//获取返回值
if(responseStr.length>0)//返回值不为空才执行下面的代码
{
array=responseStr.split('|');//根据‘|’拆分,根据自己任意特殊字符,初始化数组
positionDiv();//调用方法,定位DIV显示,具体见方法解释
document.getElementById("search_suggest").innerHTML="";//每次清空DIV内容
for(var i=0;i<array.length;i++)
{
if(array[i]!="")//项值不为空,组合DIV,每个DIV有onmouseover、onmouseout、onclick对应事件
{
document.getElementById("search_suggest").innerHTML+="<div id='item" + i + "' class='itemBg' onmouseover='beMouseOver(" + i +")' onmouseout='beMouseOut(" + i + ")' onclick='beClick(" + i + ")'>" + array[i] + "</div>";
}
}
//最后一个DIV显示 关闭 效果 onclick方法
document.getElementById("search_suggest").innerHTML+="<div class='item_button' onclick='hiddenDiv();'><font color='#999999'>关闭</font></div>";
document.getElementById("search_suggest").style.display="inline";
}
else
{
document.getElementById("search_suggest").style.display="none";
}
}
}
}
//定位DIV显示
function positionDiv()
{
var DivRef= document.getElementById("search_suggest");
DivRef.style.position = "absolute";
var t=document.getElementById("KProvince");//錯誤信息:此處需要物件
DivRef.style.top= getAbsolutePos(t).y + 20;//相对文本框的TOP高度,方法见下面
DivRef.style.left= getAbsolutePos(t).x ;//相对文本框的LEFT宽度
DivRef.style.height=array.length * 20;//DIV的高度等于每行20个象素乘行数(也就是数组的长度,体现全局数组的作用,不然要传入数组长度的参数)
}
function hiddenDiv()
{
document.getElementById("search_suggest").style.display="none";
}
//定位方法,不做解释
function getAbsolutePos(el)
{
var SL = 0, ST = 0;
var is_div = /^div$/i.test(el.tagName);
if (is_div && el.scrollLeft) SL = el.scrollLeft;
if (is_div && el.scrollTop) ST = el.scrollTop;
var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
if (el.offsetParent)
{
var tmp = this.getAbsolutePos(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
} //最后是鼠标效果的方法。
//函数鼠标经过效果
function beMouseOverEFF(i)
{
if ((i>=0)&(i<=array.length-1))
{
document.getElementById("item" + i).className="item_high";
}
} //函数鼠标移开效果
function beMouseOutEFF(i)
{
if ((i>=0)&(i<=array.length-1))
{
document.getElementById("item" + i).className="item_normal";
}
} //函数鼠标经过
function beMouseOver(i)
{
document.getElementById("KProvince").focus();
beMouseOutEFF(zz);
zz=i;
beMouseOverEFF(zz);
} //函数鼠标移开
function beMouseOut(i)
{
beMouseOutEFF(i);
}
//函数单击
function beClick(i)
{
document.getElementById("KProvince").value=array[i];
document.getElementById("search_suggest").style.display="none";
document.getElementById("KProvince").focus();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>
目前版本还不支持上下键的选择功能,还不能遮挡选择控件,下周版本将支持这一功能</h3>
<br />
<!--<input id="KProvince" autocomplete="off" onkeyup="beKeyUp();" style="position: absolute;
width: 200px; border: #666 1px solid;" />-->
<div id="search_suggest" style="position: absolute; background-color: #fff; z-index: 1;">
</div>
<br /><br /><br />
Email:[email protected]
<asp:DataGrid ID="myDataGrid" runat="server" AutoGenerateColumns="False" OnCancelCommand="myDataGrid_CancelCommand"
OnEditCommand="myDataGrid_EditCommand" Width="443px" OnUpdateCommand="myDataGrid_UpdateCommand">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="編號" ReadOnly="True">
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="名字">
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
<ItemTemplate>
<asp:Label id="lbl_Name" runat="server" Text='<%# DataBinder.Eval(Container,"Dataitem.name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="KProvince" AutoCompleteType="None" onkeyup="beKeyUp();" Text='<%# DataBinder.Eval(Container,"Dataitem.name")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn CancelText="取消" EditText="編輯" HeaderText="編輯" UpdateText="更新">
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" />
</asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
以上就是我的頁面代碼,紅色標記的地方就是得不到控件id
再用document.getElementById就可以了
而服务器端的控件会被服务器进行编译后,再输出HTML, 就是说.. 服务器控件的ID有可能不是HTML控件中的ID.
KProvince.ClientID 才是真正HTML代码中的控件ID.
alert(key);
return;
為什么key 里面的值會是undefined
如果不是,那么应该是innerText属性..
你可以尝试将二者都alert出来..试试看.