jQuery验证-在运行时更改max的值 [英] jQuery validation - changing value of max at runtime

查看:104
本文介绍了jQuery验证-在运行时更改max的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下验证规则:

$("#ed_details").validate({
    rules: {
        tagno: { required: true },
        itemid: { required: true },
        ralno: { required: true },
        feet: { required: true, number: true, min: 0 },
        inches: { required: true, number: true, min: 0 },
        cms: { required: true, number: true, min: 0 },
        length: { required: true, number: true, max: $('#maxlength').val() },
        qty: { required: true }
    }
});

这是长度验证所依据的字段.它是仅用于测试目的的文本.一旦工作,它将被隐藏.

This is the field that lengths validates against. It is text only for testing purposes. It will be hidden once this is working.

<input type="text" name="maxlength" id="maxlength" value="<?=$maxL['ml']?>" />

除非我在运行时更改maxlength的值(这可能发生),否则此方法工作正常.用户正在从下拉列表中选择一个项目.每个项目都有可使用的最大管道长度.当项目更改时,maxlength的值的确在屏幕上更改.但是,当验证运行时,我会看到一条基于原始长度的错误消息(新项目为0,无论进行编辑时加载了什么最大长度).

This works fine unless I change the value of maxlength at runtime (which can happen). The user is selecting an item from a drop down. Each item has a maximum length of tubing that can be used with it. When the item changes, the value of maxlength does change on the screen. However, when the validation runs, I see an error message based on the original length (0 for a new item, whatever the maxlength was at load for an edit).

Please enter a value less than or equal to 156.

即使maxlength字段显示另一个值,我也看到了这一点.

I see this even when the maxlength field shows another value.

如果我在该字段上使用Firebug,即使更改后它仍会显示原始值.另外,手动更改maxlength字段而不是代码也没有什么区别.

If I use Firebug on the field, it shows the original value even after it has been changed. Also, changing the maxlength field by hand instead of code does not make a difference.

非常感谢您的帮助.

推荐答案

您不能这样更改rules ...

You can not change rules like this...

length: { 
    required: true,
    number: true, 
    max: $('#maxlength').val()
},

这是因为,与大多数jQuery插件一样,.validate()仅被调用一次一个时间来初始化您的form上的插件...它不会重复运行,因此动态更改.validate()中的rules选项不会执行任何操作.

That's because, like most jQuery plugins, .validate() is called only one time to initialize the plugin on your form... it does not run repeatedly, so dynamically changing the rules options within .validate() does nothing.

使用rules方法来add并动态更新规则. (与更改值的代码一起使用.)

Use the rules method to add and update your rules dynamically. (use along with the code that changes the value.)

$('#maxlength').rules("add", {
     max: $('#maxlength').val()
});

请参见下面的这个非常粗糙的概念验证"演示(您有重新安排的空间).测试表格(初始值为5).然后单击一次按钮,值和规则(以及消息)将正确更新为"2".

See this very crude "proof of concept" demo below (room for you to rearrange). Test the form (value is initially 5). Then click the button one time and the value and rule (along with message) are updated correctly to "2".

http://jsfiddle.net/Y565M/

http://docs.jquery.com/Plugins/Validation/rules #.22add.22规则

这篇关于jQuery验证-在运行时更改max的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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