Jquery 使用动态值验证最大方法? [英] Jquery Validate Max Method With a Dynamic Value?

查看:22
本文介绍了Jquery 使用动态值验证最大方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 http://jqueryvalidation.org/max-method/ 的脚本..带有静态值的示例.如何使用最大值但动态?我试过这个脚本,但没有用

I tried the script of http://jqueryvalidation.org/max-method/.. with examples of static values. how to use the max value but dynamic? I tried with this script but it did not work

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      max: "#field1" //from field1 dynamic value???
    //max: 23 //static value
    }
  }
});

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
<form id="myform">
    <lable>value1 : </lable>
    <input type="text" class="left" id="field1" name="field1"><br>
    <lable>no greater than value1 : </lable>
    <input type="text" class="left" id="field" name="field">
      <br/>
<input type="submit" value="Validate!">
</form>

如何工作?

推荐答案

使用函数返回字段的值.

Return the value of the field using a function.

$( "#myform" ).validate({
    rules: {
        field: {
            required: true,
            max: function() {
                return parseInt($('#field1').val());
            }
        }
    }
});

记住将值括在 parseInt 内,否则数字可以作为字符串计算.

Remember to enclose the value within parseInt or the number could be evaluated as a string.

工作演示:http://jsfiddle.net/dq7wf3qj/

这篇关于Jquery 使用动态值验证最大方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆