首页 > 解决方案 > 通过类而不是元素名称将元素值传递给 jQuery.validator.addMethod

问题描述

我添加了一个方法验证插件。但是我需要使用类名来传递元素,而不是传递名称,因为我不知道id页面上呈现的元素的名称。

我尝试了创建方法并尝试使用addClassRules. params[1]问题是价值没有进来undefined

我无法弄清楚如何将元素值传递给方法。

jQuery.validator.addMethod("MyMethod", function(value, element, params) {
  alert("First Value is " + params[0]);
  alert("Second Value is " + params[1]);
  return true; //for now

  //My code to do validation
}, '{0}'); //error message will come from params[0]

jQuery.validator.addClassRules("MyClassName", {
  required: true,
  MyMethod: ['My error message', $('#secCommonInformation .DOB').val()]
});

标签: jqueryjquery-validate

解决方案


推荐阅读