一组字段的jQuery错误消息

时间:2015-11-11 08:46:04

标签: jquery

我开发了一个多字段的形式,我想为所有人显示相同的错误信息,所以我写了这个:



     // JavaScript Document
     //jQuery Form Validation code

    (function($, W, D) {
      var JQUERY4U = {};

      JQUERY4U.UTIL = {
        setupFormValidation: function() {

          var ruleSetCapacity = {
            required: true,
            checkCapacity: true
          };

          var ruleSetType = {
            required: true,
            checkType: true
          };

          $("#addFac-form").validate({
            errorElement: 'div',
            rules: {
              capacity: ruleSetCapacity,
              Lib_capacity: ruleSetCapacity,
              Cli_capacity: ruleSetCapacity,
              Sba_capacity: ruleSetCapacity,
              Gwt_capacity: ruleSetCapacity,

              Exh_type: ruleSetType,
              Toi_type: ruleSetType,
              Ga_type: ruleSetType,

            },

            // Specify the validation error messages
            messages: {
              ruleSetCapacity: {
                required: "Enter capacity",
              },
              ruleSetType: {
                required: "Enter type",
              },
            },

            submitHandler: function(form) {
              form.submit();
            }
          });

          jQuery.validator.addMethod('checkType', function(value) {
            return (value != 'none');
          }, "Select facility type");

        }
      }

      $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
      });

    })(jQuery, window, document);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
  <!-- jQuery Validation Plugin 
    hosted by Google API -->
  <!-- jQuery Library -->
  <script type="text/javascript" src="jquery.validate.min.js"></script>
  <script type="text/javascript" src="U.js"></script>
</head>

<body>
  <form action="test.php" method="post" enctype="multipart/form-data" id="addFac-form" novalidate>
    <label>Type</label>
    <select name="Exh_type">
      <option value="none"></option>
      <option value="Atrium">Atrium</option>
      <option value="Red Carpet">Red Carpet</option>
      <option value="Gallery C Ground Floor">Gallery C Ground Floor</option>
      <option value="Gallery B Ground Floor">Gallery B Ground Floor</option>
      <option value="Gallery C Third Floor">Gallery C Third Floor</option>
      <option value="Gallery B Third Floor">Gallery B Third Floor</option>
    </select>
    <br>
    <label>Capacity</label>
    <input type="text" name="Lib_capacity" dir="ltr">
    <br>
    <label>Another Type</label>
    <select name="Toi_type">
      <option value="none"></option>
      <option value="Single Surface Area">Single Surface Area</option>
      <option value="Common Surface Area">Common Surface Area</option>
    </select>
    <br>
    <input type="submit" value="Add" name="add" id="add">
  </form>
</body>

</html>
&#13;
&#13;
&#13;

问题是:

我一直在&#34;这个字段是必需的。&#34; (默认错误信息)而不是&#34;输入容量或输入类型&#34;,为什么?

请复制并运行我的代码

0 个答案:

没有答案