如何检查textarea是否包含任何内容?
我尝试使用此代码
if(document.getElementById("field").value ==null)
{
alert("debug");
document.getElementById("field").style.display ="none";
}
但它没有做我所期望的。 我希望它应该出现一个消息框“debug”,并且textarea不会显示。
我该如何解决这个问题?
答案 0 :(得分:26)
您想检查值是== ""
,而不是NULL
。
if(document.getElementById("field").value == '')
{
alert("debug");
document.getElementById("field").style.display ="none";
}
<强>更新强>
another one using TRIM如果你想确保他们不发布空格
TRIM()的实施
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
答案 1 :(得分:6)
您也可以使用以下jQuery来转义空格。
if($("#YourTextAreaID").val().trim().length < 1)
{
alert("Please Enter Text...");
return;
}
答案 2 :(得分:0)
<强> There is a world of difference between null and empty ! 强>
请改为尝试:
if(document.getElementById("field").value == "")
{
alert("debug");
document.getElementById("field").style.display ="none";
}