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

后端 未结 2 1333
挽巷
挽巷 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:41

    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.

    0 讨论(0)
  • 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);
    });
    
    0 讨论(0)
提交回复
热议问题