简体   繁体   English

jQuery确保填写所有表单字段

[英]jQuery make sure all form fields are filled

I have a simple form I'm making client side validation for. 我有一个简单的表单,我正在进行客户端验证。 To validate, none of the fields should be left blank. 要进行验证,不应将任何字段留空。 This is how I go at it: 这是我的方式:

function validateForm() {
  $('.form-field').each(function() {
    if ( $(this).val() === '' ) {
      return false
    }
    else {
      return true;
    }
  });
}

For some reason, my function always returns false, even though all fields are filled. 出于某种原因,即使填写了所有字段,我的函数也总是返回false。

You cannot return false from within the anonymous function. 您不能在匿名函数中返回false。 In addition, if it did work, you would return false if your first field was empty, true if not, and completely ignore the rest of your fields. 此外,如果它确实有效,如果第一个字段为空,则返回false,否则返回true,并完全忽略其余字段。 There may be a more elegant solution but you can do something like this: 可能有一个更优雅的解决方案,但你可以做这样的事情:

function validateForm() {
  var isValid = true;
  $('.form-field').each(function() {
    if ( $(this).val() === '' )
        isValid = false;
  });
  return isValid;
}

Another recommendation: this requires you to decorate all of your form fields with that formfield class. 另一个建议:这需要您使用该formfield类来装饰所有表单字段。 You may be interested in filtering using a different selector, eg $('form.validated-form input[type="text"]') 您可能有兴趣使用不同的选择器进行过滤,例如$('form.validated-form input[type="text"]')

EDIT Ah, I got beat to the punch, but my explanation is still valid and hopefully helpful. 编辑啊,我打了一拳,但我的解释仍然有效,希望有帮助。

You were returning from the inner function not from the validate method 您从内部函数返回而不是从validate方法返回

Try 尝试

function validateForm() {
    var valid = true;
    $('.form-field').each(function () {
        if ($(this).val() === '') {
            valid = false;
            return false;
        }
    });
    return valid
}
function validateForm() {
    var invalid= 0;
    $('.form-field').each(function () {
        if ($(this).val() == '') {
            invalid++;
        }
    });

   if(invalid>0)
       return false;
   else
       return true;
}

Here is a similar approach: 这是一个类似的方法:

function validateForm() {
  var valid = true;
  $('.form-field').each(function() {
    valid &= !!$(this).val();
  });
  return valid;
}

!! just converts input value to bool 只需将输入值转换为bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查以确保所有字段都已填写 - Check to make sure all fields are filled Jquery 如何检查表格的所有字段是否都已填写? - How to check if all the fields of a form are filled? 检查所有表单字段是否已填写-不起作用 - Check if all form fields filled - not working Vue如何查看表单中的所有字段是否已填写 - How to see if all fields are filled in a form with Vue 随机选择所有表单字段值并填写表单 - JS或jQuery - 不确定哪个 - Randomly select all form fields values and fill out form - JS or jQuery - Not sure which 如果所有字段都填写在 jQuery 中而不使用each(),如何自动提交表单? - How to automatically submit a form if all fields are filled in jQuery without using each()? 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 成功填写表单字段后如何进行Ajax调用 - How to make Ajax call after successfully filled the form fields jQuery如何确保用户已为表单上的所有选择菜单选择一个值 - jquery how to make sure that the user has selected a value for all select menus on a form 仅在填写所有表单字段时如何显示结果? - How to show result only when all form fields are filled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM