jQuery-检查输入字段的长度? [英] jquery - check length of input field?

查看:96
本文介绍了jQuery-检查输入字段的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码旨在在用户单击textarea字段后启用提交按钮.它可以工作,但是我也尝试这样做,以便仅在字段中至少有一个字符时才启用它.我尝试将其包装在:

The code below is intended to enable the submit button once the user clicks in the textarea field. It works, but I'm trying to also make it so that it's only enabled if there's at least one character in the field. I tried wrapping it in:

if ($(this).val().length > 1) 
{

}

但是,这似乎不起作用...有什么想法吗?

But, that didn't seem to work... Any ideas?

$("#fbss").focus(function () {
    $(this).select();
    if ($(this).val() == "Default text") {
        $(this).val("");
        $("input[id=fbss-submit]").removeClass();
        $("input[id=fbss-submit]").attr('disabled', false);
        $("input[id= fbss-submit]").attr('class', '.enableSubmit');
        if ($('.charsRemaining')) {
            $('.charsRemaining').remove();
            $("textarea[id=fbss]").maxlength({
                maxCharacters: 190,
                status: true,
                statusClass: 'charsRemaining',
                statusText: 'characters left',
                notificationClass: 'notification',
                showAlert: false,
                alertText: 'You have exceeded the maximum amount of characters',
                slider: false
            });

        }
    }
});

推荐答案

这不起作用,因为从代码的其余部分来看,文本输入的初始值为默认文本"-大于一个字符,因此您的if条件始终为true.

That doesn't work because, judging by the rest of the code, the initial value of the text input is "Default text" - which is more than one character, and so your if condition is always true.

在我看来,最简单的方法可以解决这种情况:

The simplest way to make it work, it seems to me, is to account for this case:

    var value = $(this).val();
    if ( value.length > 0 && value != "Default text" ) ...

这篇关于jQuery-检查输入字段的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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