JQuery-Validation - using rules method on selected ID, why does the name attribute have to be present on the element?

删除回忆录丶 提交于 2019-11-29 11:59:23

A name attribute is required on the element firstly because that's what jQuery validate uses internally as a key for each field, and secondly because the name attribute is required on input elements to ensure the page validates to the specified DOCTYPE.

If you want to apply the rules using the id then use like this,

$(function () {
  var $field = $("#id_field").attr("name");
  var $params = {debug:false, rules:{}, messages:{}};
  $params['rules'][$field] = {"required": true, "rule": ["params"]};
  $params['messages'][$field] = "error message";

  $("#frm").validate($params);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!