3

我有以下脚本来验证我的表单。它工作正常,但我试图在内部<span class="errormsg"></span>以逗号分隔显示所有验证错误消息,而不是在每个输入旁边单独显示它们。

我对jquery知之甚少;我不知道如何实现这一点。你能告诉我吗?

谢谢 :)

<script>
  $(document).ready(function() { 
    // validate signup form on keyup and submit 
    var validator = $("#signupform").validate({
      rules: {
  customerid: "required",
  date: "required",
  invoiceid: {
    required: true,
    minlength: 2,
    remote: "<? echo base_url();?>mycontroller/function" }
  },
      messages: {
    customerid: "Enter your customerid",
    date: "Enter your date",
    invoiceid: {
       required: "Enter a invoiceid",
       minlength: jQuery.format("Enter at least {0} characters"),
       remote: jQuery.format("{0} is already in use")
    },
      }
    });
  });
</script>

我的表格

<label>Invoice #</label>
<input type="text" name="invoiceid" id="invoiceid" value="" />

<label  style="margin-left:20px;">Date</label>
<input type="text" id="datepicker1" name="date" value=""  />

<label  style="margin-left:20px;">Customer</label>
<input type="text" id="customerid" name="customerid" value=""  />
4

1 回答 1

13

我会使用该showErrors选项来完成此操作:

showErrors: function(errorMap, errorList) {
    $(".errormsg").html($.map(errorList, function (el) {
        return el.message;
    }).join(", "));
},

示例:http: //jsfiddle.net/bd3zM/

笔记:

  • 用于根据传递给函数$.map的参数构建错误消息数组。errorList
  • 然后用于.join(", ")将上述数组连接成一个字符串。
  • errormsg使用类将内容放置在元素内html
于 2012-05-28T18:08:45.487 回答