如何在Javascript或Jquery中检查Textarea是否为空? [英] How to check if a Textarea is empty in Javascript or Jquery?

查看:85
本文介绍了如何在Javascript或Jquery中检查Textarea是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查textarea是否包含任何内容?

How do i check if a textarea contains nothing?

我尝试使用此代码

if(document.getElementById("field").value ==null)
{
    alert("debug");
    document.getElementById("field").style.display ="none";
 }

但它并没有按我的预期行事。
我希望它应该出现一个消息框debug并且textarea没有显示。

But it doesnt do what i expect. I expect that it should appear a messagebox "debug" and that the textarea is not shown.

我该如何解决这个问题?

How can i fix that issue?

推荐答案

你想检查价值是否为 ==,而不是 NULL

You wanna check if the value is == "", not NULL.

if(document.getElementById("field").value == '')
{
    alert("debug");
    document.getElementById("field").style.display ="none";
}

更新

工作示例

并且使用TRIM的另一个,以防你想确保他们不发布空格

And another one using TRIM in case you wanna make sure they don't post spaces

TRIM的实现()

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
}

这篇关于如何在Javascript或Jquery中检查Textarea是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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