jQuery - 简单的输入验证 - “空”和“非空”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6801483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:24:21  来源:igfitidea点击:

jQuery - simple input validation - "empty" and "not empty"

jqueryajaxvalidation

提问by Wordpressor

I have an input that I want to validate:

我有一个要验证的输入:

<input type="text" id="input" />

And here's the JS:

这是JS:

        jQuery("#input").live('change', function() {

            if("#input:not(:empty)") {
                alert('not empty');
            }   

            else if("#input:empty") {
                alert('empty'); 
            }
        });

Even when the "#input" is empty there's no way of displaying alert with "empty" message. So, basically, no matter what I do, only the first statement is true and the second is false, always.

即使“#input”为空,也无法显示带有“空”消息的警报。所以,基本上,无论我做什么,总是只有第一个陈述是正确的,第二个陈述是错误的。

What's wrong?

怎么了?

回答by Gordon Gustafson

JQuery's :empty selectorselects all elements on the page that are empty in the sense that they have no child elements, including text nodes, not all inputs that have no text in them.

JQuery 的 :empty 选择器选择页面上所有空元素,因为它们没有子元素,包括文本节点,而不是所有没有文本的输入。

Jquery: How to check if an input element has not been filled in.

Jquery:如何检查输入元素是否未填写。

Here's the code stolen from the above thread:

这是从上述线程中窃取的代码:

$('#apply-form input').blur(function()          //whenever you click off an input element
{                   
    if( !$(this).val() ) {                      //if it is blank. 
         alert('empty');    
    }
});

This works because an empty string in JavaScript is a 'falsy value', which basically means if you try to use it as a boolean value it will always evaluate to false. If you want, you can change the conditional to $(this).val() === ''for added clarity. :D

这是有效的,因为 JavaScript 中的空字符串是一个“假值”,这基本上意味着如果您尝试将它用作布尔值,它将始终评估为false. 如果需要,您可以将条件更改为$(this).val() === ''以增加清晰度。:D

回答by Jason Gennaro

You could do this

你可以这样做

$("#input").blur(function(){
    if($(this).val() == ''){
        alert('empty'); 
    }
});

http://jsfiddle.net/jasongennaro/Y5P9k/1/

http://jsfiddle.net/jasonennaro/Y5P9k/1/

When the input has lost focusthat is .blur(), then check the value of the #input.

当输入丢失时focus,即.blur()检查 的值#input

If it is empty == ''then trigger the alert.

如果它是空的,== ''则触发警报。

回答by Willem

    jQuery("#input").live('change', function() {
        // since we check more than once against the value, place it in a var.
        var inputvalue = $("#input").attr("value");

        // if it's value **IS NOT** ""
        if(inputvalue !== "") {
            jQuery(this).css('outline', 'solid 1px red'); 
        }   

        // else if it's value **IS** ""
        else if(inputvalue === "") {
            alert('empty'); 
        }

    });

回答by Mcloide

Actually there is a simpler way to do this, just:

其实有一个更简单的方法来做到这一点,只是:

if ($("#input").is('empty')) {
console.log('empty');
} else {
console.log('not empty');
}