[英]How to find all child elements with a class and check if any are empty?
I have a form and need to check to make sure any child inputs of a fieldset are empty. 我有一个表格,需要检查以确保字段集的所有子输入为空。 How can I accomplish this using jQuery?
如何使用jQuery完成此操作?
if any fields with .required are empty -> there are fields that have not been filled out if all fields with .required have been filed out -> they have all been filed out 如果任何带有.required的字段为空->如果所有带有.required的字段都已被归档->有些字段尚未填写->它们都已被归档
Select your fields by form id and class name. 通过表单ID和类名称选择字段。 Then use
.filter()
to reduce your set to include only the fields that have not been filled out: 然后使用
.filter()
减少您的设置,使其仅包括尚未填写的字段:
var emptyFields = $("#myForm .required").filter(function()
{
return $(this).val() == "";
});
If you don't want to do anything with the fields, but just need a boolean: 如果您不想对字段做任何事情,而只需要一个布尔值:
var anyEmptyFields = !!emptyFields.length;
尝试这个:
var emptyRequiredFields = jQuery("#post-form .required:input").not("[value]");
Try this: 尝试这个:
<fieldset id="myForm">
<input type="text" required="true" /><br />
<input type="check" required="true" /><br />
<input type="radio" required="true" /><br />
<input type="text" required="false" /><br />
<input type="check" required="" /><br />
<input type="radio" /><br />
<button onclick="$('#myForm').find('input[required=true]').after('<span>null</span>');">Check</button>
</fieldset>
Try this one:
试试这个:
$("form[name=form_name] .required").each(function(index,e){
if(e.value == ''){
alert(e.name+' field is required!');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.