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

后端 未结 2 1332
挽巷
挽巷 2020-12-20 03:04

I\'m a little confused about the JQuery validation plugin behavior.

If I have the following JQuery:

$(\'#form1\').validate({
    /* other validation          


        
2条回答
  •  时光说笑
    2020-12-20 03:53

    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);
    });
    

提交回复
热议问题