Javascript 在javascript中获取文本框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6069146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:06:37  来源:igfitidea点击:

To get value of textbox in javascript

javascript

提问by Microsoft Developer

I dont know why my script is not working. I tried to get value of textbox in javascript but its not giving me a value. Value is shown as null.

我不知道为什么我的脚本不起作用。我试图在 javascript 中获取文本框的值,但它没有给我一个值。值显示为空。

//This is the code for my textbox

    <td> <input id="demo3" type="text" size="25" runat="server"/><a href="javascript:NewCal('demo3','ddmmmyyyy',true,24)">
     <img src="Resources/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>   </td>

//This is how I trid to access textbox
    exDateTime = document.getElementById('pCtrl').value;

but exDateTime is remains null. I tried using other ways too, but nothing seen happening..

但 exDateTime 仍然为空。我也尝试使用其他方式,但没有看到任何事情发生..

采纳答案by Ashwin Krishnamurthy

I don't think you are referring to the proper id. Try document.getElementById("demo3").value

我不认为您指的是正确的 ID。尝试document.getElementById("demo3").value

Or atleast paste the code where your "pCtrl" form field is.

或者至少将代码粘贴到“pCtrl”表单字段所在的位置。

EDIT :Make sure you set the value for the text box in html or using Javascript for example:

编辑:确保您在 html 或使用 Javascript 中设置文本框的值,例如:

<input type='text' id='demo3' value='3'> 

and then use document.getElementById("demo3").valueto get the value, otherwise use javascript to assign the value and obtain the value at a later stage using the same.

然后使用document.getElementById("demo3").value获取值,否则使用javascript分配值并在稍后使用相同的阶段获取值。

I hope this helped you.

我希望这对你有帮助。

回答by mplungjan

I bet this is a .net issue

我敢打赌这是一个 .net 问题

Try this:

尝试这个:

<input id="demo3" type="text" size="25" runat="server"/><a 
href="#" onclick="NewCal('<%= demo3.ClientID %>','ddmmmyyyy',true,24); 
return false"><img src="Resources/cal.gif" width="16" height="16" border="0" 
alt="Pick a date"></a>   


exDateTime = document.getElementById('<%= demo3.ClientID %>').value;