jQuery检测输入字段为空 [英] jQuery detect input fields are blank

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

问题描述

使用jQuery如何检查所有输入字段(EXCL隐藏字段)和textareas是否为空白?我不确定如何在所有表单字段中循环"并检查:

Using jQuery how can check if all input fields (EXCL hidden fields) and textareas are blank? I'm not sure how to "loop" through all form fields and check for:

$("input").val() 和 $("textarea).val()

$("input").val() and $("textarea).val()

这是要运行一个仅在所有表单字段均为空白时才起作用"的功能.

This is to run a function that should only "work" if all form fields are blank.

推荐答案

请选择,但此处仅需几行即可验证所有字段,而不仅仅是一个:

Take your pick, but here it is in just a few lines to validate all the fields, not just one:

var allBlank = true;
$('input, textarea').each(function() {
    return allBlank = allBlank && !$(this).val();
}); 

自然,您可以修改选择器以仅指定所需的字段.然后继续:

Naturally you can modify the selector to specify just the fields you want. Then go on to:

if(allBlank) {
    //all are blank.  congrats. 
}

现在具有更高的性能!

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

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