admin管理员组文章数量:1290941
I have an asp web form with a couple of asp textbox controls:
<asp:TextBox ID="txtTextBox" runat="server" /> .
I have a javascript file tools.js that are included in the page:
<script src="tools.js" type="text/javascript"></script>
How can I access the value from txtTextBox from javascript?
Ive tried using
document.getElementById('<%= txtTextBox.ClienID %>').value;
document.getElementById('<%= txtTextBox.UniqueID %>').value;
document.getElementById('<%= txtTextBox %>').value;
but none of them works.
Any ideas?
I have an asp web form with a couple of asp textbox controls:
<asp:TextBox ID="txtTextBox" runat="server" /> .
I have a javascript file tools.js that are included in the page:
<script src="tools.js" type="text/javascript"></script>
How can I access the value from txtTextBox from javascript?
Ive tried using
document.getElementById('<%= txtTextBox.ClienID %>').value;
document.getElementById('<%= txtTextBox.UniqueID %>').value;
document.getElementById('<%= txtTextBox %>').value;
but none of them works.
Any ideas?
Share Improve this question asked May 5, 2010 at 9:58 Tomas VinterTomas Vinter 2,6919 gold badges36 silver badges45 bronze badges2 Answers
Reset to default 5You're missing a t
on the ClientID
(correct!) solution :), like this:
document.getElementById('<%= txtTextBox.ClientID %>').value;
Note though the above code has to be in the page so it prints out like this to the client:
document.getElementById('txtTextBox').value;
//or if it's deeper:
document.getElementById('Parent_txtTextBox').value;
If it's in an external js, then it prints out literally like this, without replacing the <%= %>
part:
document.getElementById('<%= txtTextBox.ClientID %>').value;
Since there's no <input id="<%= txtTextBox.ClientID %>" />
, that doesn't work :)
You cant access the txtTextBox.ClientID from your js-file becase its an ASP.Net Property. What are you doing with the textboxes' value? Consider giving the js-function the value or the reference to the textbox per parameter in this way(f.e. onchange-event):
<asp:TextBox ID="txtTextBox" runat="server" onchange="javascript: callFunction( this );" />
where callFuntion is your js-function in tools.js:
function callFunction( txtBox ){
var txtBoxValue;
if(txtBox != null)
txtBoxValue = txtBox.value;
}
Regards, Tim
本文标签: How to retrieve value from aspnet textbox from javascriptStack Overflow
版权声明:本文标题:How to retrieve value from asp.net textbox from javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520930a2383179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论