jQuery检测textarea是否为空 [英] jQuery detect if textarea is empty

查看:163
本文介绍了jQuery检测textarea是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定如果用户使用jQuery删除了文本区域中预先填充的内容,则文本区域是否为空.

I am trying to determine if a textarea is empty if a user deletes what is already pre-populated in the textarea using jQuery.

是否要这样做?

这是我所拥有的,并且无法正常工作

This is what I have and its not working

$(textarea).live('change', function(){
            if($(this).val().length == 0) {
                $(submitButtonClass).addClass('disabled_button');
                $(submitButtonClass).removeClass('transSubmit');
            } else {
                $('.disabled_button').addClass('transSubmit').css({
                    'cursor':'pointer'
                }).removeClass('disabled_button');
            }
        });

推荐答案

if (!$("#myTextArea").val()) {
    // textarea is empty
}

您还可以使用 $.trim 来确保元素不只包含空格:

You can also use $.trim to make sure the element doesn't contain only white-space:

if (!$.trim($("#myTextArea").val())) {
    // textarea is empty or contains only white-space
}

这篇关于jQuery检测textarea是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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