当元素是空文本框时,Javascript document.getElementById("id").value 返回 null 而不是空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6491483/
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
Javascript document.getElementById("id").value returning null instead of empty string when the element is an empty text box
提问by Guruprasad
I have a text box element whose value I am trying to access using document.getElementById("id-name").value
. I find that the call is returning a null instead of empty string. The data-type of the returned value is still string. Is null a string value?
我有一个文本框元素,我试图使用document.getElementById("id-name").value
. 我发现调用返回的是 null 而不是空字符串。返回值的数据类型仍然是字符串。null 是字符串值吗?
<input type="text" value="" id="mytext">
is the textbox whose value I am trying to fetch using var mytextvalue = document.getElementById("mytext").value;
<input type="text" value="" id="mytext">
是我试图使用的值的文本框 var mytextvalue = document.getElementById("mytext").value;
采纳答案by ilight
Posting your HTML might help a bit. Instead, you can get the element first and then check if it is null or not and then ask for its value rather than just asking for the value directly without knowing if the element is visible on the HTML or not.
发布您的 HTML 可能会有所帮助。相反,您可以先获取元素,然后检查它是否为空,然后询问其值,而不是直接询问该值而不知道该元素是否在 HTML 上可见。
element1 = document.getElementById(id);
if(element1 != null)
{
//code to set the value variable.
}
回答by Ivan Town
fyi, this can happen if you are using the html type="number" attribute on your input tag. Entering a non-number will clear it before your script knows what's going on.
仅供参考,如果您在输入标签上使用 html type="number" 属性,就会发生这种情况。在您的脚本知道发生了什么之前,输入一个非数字将清除它。
回答by user4989959
For your code
对于您的代码
var mytextvalue = document.getElementById("mytext");
mytextvalue
will contain null
if you have a document.write()
statement before this code. So remove the document.write
statement and you should get a proper text object in the variable mytextvalue
.
mytextvalue
null
如果您document.write()
在此代码之前有一个语句,则将包含。因此删除该document.write
语句,您应该在变量中获得一个正确的文本对象mytextvalue
。
This is caused by document.write
changing the document.
这是由document.write
更改文档引起的。
回答by Aziz Shaikh
Please check this fiddleand let me know if you get an alert of null value. I have copied your code there and added a couple of alerts. Just like others, I also dont see a null being returned, I get an empty string. Which browser are you using?
请检查这个小提琴,如果您收到空值警报,请告诉我。我已经在那里复制了您的代码并添加了一些警报。就像其他人一样,我也没有看到返回空值,我得到一个空字符串。您使用的是哪个浏览器?
回答by andyb
This demois returning correctly for me in Chrome 14, FF3 and FF5 (with Firebug):
这个演示在 Chrome 14、FF3 和 FF5(使用 Firebug)中为我正确返回:
var mytextvalue = document.getElementById("mytext").value;
console.log(mytextvalue == ''); // true
console.log(mytextvalue == null); // false
and changing the console.log
to alert
, I still get the desired output in IE6.
并更改console.log
为alert
,我仍然在 IE6 中获得所需的输出。
回答by ilight
I think the textbox you are trying to access is not yet loaded onto the page at the time your javascript is being executed.
我认为在执行 javascript 时,您尝试访问的文本框尚未加载到页面上。
ie., For the Javascript to be able to read the textbox from the DOM of the page, the textbox must be available as an element. If the javascript is getting called before the textbox is written onto the page, the textbox will not be visible and so NULL is returned.
即,为了使 Javascript 能够从页面的 DOM 读取文本框,文本框必须作为元素可用。如果在将文本框写入页面之前调用 javascript,则文本框将不可见,因此返回 NULL。
回答by Rahul
try this...
<script type="text/javascript">
function test(){
var av=document.getElementById("mytext").value;
alert(av);
}
</script>
<input type="text" value="" id="mytext">
<input type="button" onclick="test()" value="go" />
回答by Pavel Koryagin
It seems that you've omitted the value attribute in HTML markup.
您似乎在 HTML 标记中省略了 value 属性。
Add it there as <input value="" ... >
.
将其添加为<input value="" ... >
.