jQuery Validator depends method undefined

隐身守侯 提交于 2019-12-18 08:57:03

问题


Firebug is showing me the following:

From the followin Validator initialization:

$("#surveyForm").validate({
    errorPlacement: function(error, element) { 
    if ( element.is(":radio") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' ); 
        } else if ( element.is(":checkbox") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' );
        } else {
        error.appendTo( element.parent() );
        }
},
    rules: {
     ans_23: {
         depends: function(element) {
           return $("#ans_22:checked")
         }
 }
   },
   debug: true
});

The rule is based on the second example under rules here.

The HTML being referenced looks like this

<td class='two_columns'>
<label>
<input type='radio' name='rad_22' id='ans_22' class='required' value='Yes'  />  Other
</label>
<input type='text' name='ans_23' id='ans_23' value='' class='' />
</td>

Anyone know why the depends method would be undefined?

Footnote: I also tried doing this using the rule add method (see below). That form validated and threw no errors when validate() was called and ans_23 did not gain "required" class....

$("#surveyForm").rules("add", {
  ans_23: {
    required: "#ans_22:checked"
  }
});

回答1:


I saw a comment reply from you to convert my comment to an answer... so here goes:

$("#surveyForm").validate({
    errorPlacement: function(error, element) { 
    if ( element.is(":radio") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' ); 
        } else if ( element.is(":checkbox") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' );
        } else {
        error.appendTo( element.parent() );
        }
},
    rules: {
     ans_23: {
         required: {
           depends: function(element) {
             return $("#ans_22:checked");
           }
         }
 }
   },
   debug: true
});


来源:https://stackoverflow.com/questions/9026740/jquery-validator-depends-method-undefined

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