Javascript 如何检查表单元素是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2469999/
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-23 00:30:13 来源:igfitidea点击:
How to check if form elements are not empty?
提问by trrrrrrm
How can I check if all the elements inside a form "textbox, checkbox, textarea, select, file" are not empty?
如何检查表单“文本框、复选框、文本区域、选择、文件”中的所有元素是否不为空?
回答by Nick Craver
You can see if any are empty like this:
您可以像这样查看是否有空:
$(":input").each(function() {
if($(this).val() === "")
alert("Empty Fields!!");
});
You can read on the :input selector here
for a more specific answer, if you only want those types, change the selector like this:
对于更具体的答案,如果您只想要这些类型,请像这样更改选择器:
$(":text, :file, :checkbox, select, textarea").each(function() {
if($(this).val() === "")
alert("Empty Fields!!");
});

