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

查看:123
本文介绍了如何使用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天全站免登陆