0

如果未填写 e_usagelow 字段,则以下 jQuery 验证代码应该检查用户必须输入 e_usage 字段的表单。我根据 jQuery Validation 中的示例编写了 e-usage 的依赖调用函数- 两个字段,只需要填写一个,但不知何故我的功能不起作用(例如,当 e_usagelow 为空时将鼠标放在 e-usage 字段上时,不会出现验证错误警告并且控制台显示值'true')

出了什么问题?

请您的帮助。

html代码:

E-usage: <input id="e_usage" type="text" name="e_usage">
E-usage low: <input id="e_usagelow" type="text" name="e_usagelow" >

javascript代码:

$(document).ready(function () {
  $("#myForm").validate({
    rules: {
      'e_usage': {
        required: function (element) {
          console.log("validation: required: " + $("#e_usagelow").is(':empty'));
          return $("#e_usagelow").is(':empty');
        },
        minlength: 3,
        maxlength: 8,
        number: true
      },
      'e_usagelow': {
        required: true,
        minlength: 3,
        maxlength: 8,
        number: true
      }
    } //rules
  }); //validate()
}); //function
4

1 回答 1

0

演示

更改'e_usage'e_usage

'e_usagelow'_e_usagelow

id不包含在引号中

$(document).ready(function () {

    $("#myForm").validate({

        rules: {
            e_usage: { //removed quotes from id
                required: function (element) {
                    console.log("validation: required: " + $("#e_usagelow").is(':empty'));
                    return $("#e_usagelow").is(':empty');
                },
                minlength: 3,
                maxlength: 8,
                number: true
            },

            e_usagelow: { //removed quotes from id
                required: true,
                minlength: 3,
                maxlength: 8,
                number: true
            }

        } //rules


    }); //validate()

});
于 2013-08-20T15:46:38.090 回答