如何使用jQuery检查所有输入是否为空 [英] How to check if all inputs are not empty with jQuery

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

问题描述

我需要使用 jQuery 验证表单.我可以一一检查我的所有输入,但这不是一个非常实用的解决方案.

I need to validate a form with jQuery. I can check all my inputs one by one, but it's not a very practical solution.

如何更有效地检查我的所有输入是否为非空?在我的表单中,我可以有不同类型的 input 元素:文本、几组收音机、选择等.

How can i check if all my inputs are non-empty more efficiently? In my form i can have input elements of different types: text, several groups of radio, select etc.

推荐答案

只需使用:

$("input:empty").length == 0;

如果为零,则没有一个是空的.

If it's zero, none are empty.

为了更聪明一点,同时过滤掉只有空格的项目,你可以这样做:

To be a bit smarter though and also filter out items with just spaces in, you could do:

$("input").filter(function () {
    return $.trim($(this).val()).length == 0
}).length == 0;

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

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