1

我对我的字段使用自定义验证

<div class="control-group">
  <label class="control-label" for="region">Region*</label>
  <div class="controls">
    <input type="text" class="input-xlarge" id="region" name="region" minlength="1" maxlength="50" autocomplete="off">
  </div>
</div>

var regionsList = ["value1", "value2", ..., "value 80"];

$.validator.addMethod("validRegion", function (value, element, param) {
  return this.optional(element) || (param.indexOf(value) != -1); // ERROR IS HERE
}, "Please start to type and choose correct value");

$("#myform").validate({
  rules: {
    "region": {
      required: true,
      validRegion: regionsList
    },
 ...

这行有什么问题(请查找带有注释 ERROR IS HERE 的行)?错误是Object doesn't support this property or method

4

1 回答 1

4

Array.indexOf仅在 IE 9+ 上受支持。由于您已经在使用 jQuery,因此您可以使用$.inArray(如果支持,它将使用Array.indexOf)。

$.validator.addMethod("validRegion", function (value, element, param) {
  return this.optional(element) || ($.inArray(value, param) != -1);
}, "Please start to type and choose correct value");
于 2012-10-18T17:36:14.583 回答