如何使用 jQuery 检查所有表单字段是否包含值

发布于 2025-01-08 05:12:43 字数 130 浏览 2 评论 0原文

我需要知道是否所有字段都有值,如果有,我想激活提交按钮。

我看过插件,但我不知道我需要/应该使用哪一个。

该表单使用输入文本框下拉选择

I need away to know if all fields have a value and if they do, I want to activate the submit button.

I have looked at plugins but I don't know which one I need/should use.

The form uses input, textbox and dropdown selects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2025-01-15 05:12:43

如果你不想使用插件,你可以做一些简单的事情,比如这样(注意:我还没有测试过这个):

/**
 * Checks to see if all of the values in a form are filled in.
 *
 * @param formId String the id of the form to check (without the #)
 * @return boolean True indicates all form fields are filled in
 */
function checkForm(formId){

  var isFormComplete = true;

  $('#' + formId).find('input,select,textarea').each(function(){
      var formValue = jQuery.trim($(this).val());

      if(!formValue){
          isFormComplete = false;
      }
  });

  return isFormComplete;
}

然后在某些事件上(可能是 focusblur) 您调用此函数,并根据返回值切换提交按钮。

If you don't want to use a plugin you could do something simple like this (note: I haven't tested this):

/**
 * Checks to see if all of the values in a form are filled in.
 *
 * @param formId String the id of the form to check (without the #)
 * @return boolean True indicates all form fields are filled in
 */
function checkForm(formId){

  var isFormComplete = true;

  $('#' + formId).find('input,select,textarea').each(function(){
      var formValue = jQuery.trim($(this).val());

      if(!formValue){
          isFormComplete = false;
      }
  });

  return isFormComplete;
}

and then on some event (probably focus or blur) you call this function, and toggle the submit button based on the return value.

眼眸印温柔 2025-01-15 05:12:43

你可以使用jquery通过控件的id来选择你想要的控件。我建议这样做而不是必须支持插件——这对你来说本质上可能是一个黑匣子。

标记:

<input type="text" id="myTextBox" />
 <input type="text" id="x" />
 <select id="y" />

jquery:

var valid = $("#myTextBox").val() != "" &&
            $("#x").val() != "" &&
            $("#y").selectedIndex != 0; // if zero would be invalid for a select...

您可以根据有效启用/禁用提交按钮。

You can use jquery to select the controls you want via their id. I would suggest this over having to support a plugin--which could essentially be a black box to you.

markup:

<input type="text" id="myTextBox" />
 <input type="text" id="x" />
 <select id="y" />

jquery:

var valid = $("#myTextBox").val() != "" &&
            $("#x").val() != "" &&
            $("#y").selectedIndex != 0; // if zero would be invalid for a select...

You can enable / disable your submit button based on valid.

雄赳赳气昂昂 2025-01-15 05:12:43
var $submit = $('#a-form').find('input[type="submit"]'),
    $inputs = $('#a-form').find('input, textarea').not('[type="submit"], [type="hidden"]');
$inputs.on('change', function () {
    var error = false;
    $.each($inputs, function (index, element) {
        if ($(this).val() == '') {
            error = true;
            $(this).css({ backgroundColor : 'red' });
        } else {
            $(this).css({ backgroundColor : 'white' });
        }
    });
    if (error) {
        $submit.prop('disabled', true);
    } else {
        $submit.prop('disabled', false);
    }
});​

这会将事件处理程序绑定到 change 事件的输入表单,该事件检查表单中每个输入的值,如果有空白,则禁用提交按钮。它还会将任何空白输入的背景颜色更改为红色

这是一个演示: http://jsfiddle.net/2zjbM/1/

var $submit = $('#a-form').find('input[type="submit"]'),
    $inputs = $('#a-form').find('input, textarea').not('[type="submit"], [type="hidden"]');
$inputs.on('change', function () {
    var error = false;
    $.each($inputs, function (index, element) {
        if ($(this).val() == '') {
            error = true;
            $(this).css({ backgroundColor : 'red' });
        } else {
            $(this).css({ backgroundColor : 'white' });
        }
    });
    if (error) {
        $submit.prop('disabled', true);
    } else {
        $submit.prop('disabled', false);
    }
});​

This binds an event handler to the inputs a form for the change event that checks the value of each input in the form, if any are blank then the submit button is disabled. It also changes the background-color of any blank inputs to red.

Here is a demo: http://jsfiddle.net/2zjbM/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文