[英]document.GetElementById doesn't work in any kind of browser
I'm trying to get the value of an input (text/textarea) and document.getElementById('rtext').value
returns nothing. 我正在尝试获取输入(text / textarea)和
document.getElementById('rtext').value
什么都不返回。 If I set a default value (value="XXX"), it returns that value, eve if i edit it. 如果我设置了默认值(value =“XXX”),则返回该值,如果我编辑它,则返回。
Here's some sample: 这是一些示例:
<div class="forms">
<p><textarea id='rtext'></textarea></p>
</div>
I'm mentioning that the form element <textarea>
isn't part of any form and i'm forced to use only document.getElementById
. 我提到表单元素
<textarea>
不是任何表单的一部分,我被迫只使用document.getElementById
。 No jQuery and no this.form.Id.value
没有jQuery,也没有
this.form.Id.value
Please help. 请帮忙。 THX
谢谢
getElementById() (case is important) is a member of the document object. getElementById() (大小写很重要)是文档对象的成员。 To call the function, you need to refer to the object and then the function.
要调用该函数,您需要引用该对象,然后引用该函数。 In dot notation, it should be:
点符号,应该是:
document.getElementById()
document.getElementById() - MDC document.getElementById() - MDC
Note that IDs must be unique on the page and having another element with the same ID might be causing your issue. 请注意,页面上的ID 必须是唯一的 ,并且具有相同ID的其他元素可能会导致您的问题。
好吧,它是document.getElementById()
,innit?
Works for me: 适合我:
<div class="forms">
<p><textarea id='rtext'></textarea></p>
</div>
<input type = "button"
onclick = "alert(document.getElementById('rtext').value);"
value = "run" />
<a href = "javascript:void(0);"
onclick = "alert(document.getElementById('rtext').value)"
>GET</a>
Things to consider: 需要考虑的事项:
getElementById()
and getElementByID()
are not the same function getElementById()
和getElementByID()
不是同一个函数 onkeyup
vs onkeypress
) onkeyup
vs onkeypress
)填充后尝试获取值 Try to put your code snippet at the end of your <body>
tag. 尝试将代码段放在
<body>
标记的末尾。 Your javascript is probably executing before your <textarea>
element has loaded. 您的javascript可能在
<textarea>
元素加载之前执行。
So: 所以:
<html>
<body>
<div class="forms">
<p><textarea id='rtext'></textarea></p>
</div>
<script type="text/javascript">
var el = document.getElementById('rtext');
alert(el);
</script>
</body>
</html>
(I've left out the head and other tags to keep it compact :) (我遗漏了头部和其他标签以保持紧凑:)
I solved the problem: 我解决了这个问题:
I inserted the form tags, I modified the 我插入了表单标签,我修改了
<a href="javascript"
onclick="alert(document.getElementById('rname').value)">GET</a>
with 同
<input type="button"
onclick="document.getElementById('rname').value=this.form.rname.value" />`
It works fine now. 它现在工作正常。
你有没有尝试过:
document.getElementById('rtext').textContent
Since you've tried everyone's solutions and you can't find the error I would suggest that you try debugging the JavaScript in your browser. 既然你已经尝试过每个人的解决方案而你找不到错误,我建议你尝试在浏览器中调试JavaScript。
In IE8 (I don't remember if 7 had it) hit F12 and bring up the developer toolbar. 在IE8中(我不记得7是否有它)点击F12并调出开发人员工具栏。 On the script tab you can test your javascript by hand in the console or debug it.
在脚本选项卡上,您可以在控制台中手动测试您的javascript或进行调试。
In FireFox use Firebug. 在FireFox中使用Firebug。
In Chrome hit Ctrl+Shift+J, the console will be at the bottom of the screen. 在Chrome中按Ctrl + Shift + J,控制台将位于屏幕底部。 You can also debug the scripts on the scripts tab.
您还可以在脚本选项卡上调试脚本。
If you are using the Text element within an Update Panel then the Id being rendered on the page is no longer rtext .Only if you are using an Update Panel. 如果您在更新面板中使用Text元素,则在页面上呈现的Id不再是rtext。仅在您使用更新面板时。 DO a view source code to verify if the ID is retained as rtext
查看源代码以验证ID是否保留为rtext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.