Javascript document.getElementById(“id”)。当元素为空文本框时,返回null而不是空字符串 [英] Javascript document.getElementById("id").value returning null instead of empty string when the element is an empty text box

查看:516
本文介绍了Javascript document.getElementById(“id”)。当元素为空文本框时,返回null而不是空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框元素,其值正在尝试使用 document.getElementById(id-name)。value 。我发现调用返回null而不是空字符串。返回值的数据类型仍然是字符串。 null是一个字符串值?

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?

< input type =textvalue =id =mytext> 是我正在尝试使用 var mytextvalue = document.getElementById(mytext)获取的值的文本框。value;

推荐答案

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

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.

element1 = document.getElementById(id);

if(element1 != null)
{
    //code to set the value variable.
}

这篇关于Javascript document.getElementById(“id”)。当元素为空文本框时,返回null而不是空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆