Javascript document.getElementById("id").当元素为空文本框时,返回null而不是空字符串

Gur*_*sad 8 javascript string null dom

我有一个文本框元素,我试图使用它的值document.getElementById("id-name").value.我发现调用返回null而不是空字符串.返回值的数据类型仍为字符串.null是字符串值吗?

<input type="text" value="" id="mytext"> 是我试图获取其值的文本框 var mytextvalue = document.getElementById("mytext").value;

ili*_*ght 7

发布您的HTML可能会有所帮助.相反,您可以先获取元素,然后检查它是否为null,然后询问其值,而不是直接询问值而不知道该元素是否在HTML上可见.

element1 = document.getElementById(id);

if(element1 != null)
{
    //code to set the value variable.
}
Run Code Online (Sandbox Code Playgroud)