1

我使用 jQuery Validation 插件来确保用户在第一个文本字段中输入了正的cost_of_car ,在第二个文本字段中输入了正的payment_amount,例如 *cost_of_car * 0.4 <= payment_amount <= cost_of_car*:

$.validator.addMethod("testMaxAmount", function()
{
    return $("#cost_of_car").val() - 0 >= $("#payment_amount").val() - 0;
}, "Can't be more than cost_of_car");

$.validator.addMethod("testMinAmount", function()
{
    return $("#cost_of_car").val() * 0.4 <= $("#payment_amount").val() - 0;
}, "Can't be less than cost_of_car * 0.4");

$("#calc_form").validate(
{
    rules: 
    {
        cost_of_car: 
        {
            required: true,
            number: true,
            min: 1,
            max: 10000000
        },
        payment_amount: 
        {
            required: true,
            number: true,
            testMaxAmount: true,
            testMinAmount: true 
        }
    }
});

现在我想跳过 testMaxAmount 和 testMinAmount 检查,直到cost_of_car有效。测试

$("#calc_form").validate().element("#cost_of_car")

甚至

$("#calc_form").validate({ignore: "#payment_amount"}).element("#cost_of_car")

在这些方法内部导致递归并挂起浏览器。

请您提出一些其他方法来禁用payment_amount的验证,直到cost_of_car有效?

4

1 回答 1

2

更新:更改必须在validator.addMethod()调用中:

$.validator.addMethod("testMaxAmount", function()
{
    if($("#cost_of_car").valid())
      return $("#cost_of_car").val() - 0 >= $("#payment_amount").val() - 0;
    else
      return true;
}, "Can't be more than cost_of_car");

$.validator.addMethod("testMinAmount", function()
{
    if($("#cost_of_car").valid())
      return $("#cost_of_car").val() * 0.4 <= $("#payment_amount").val() - 0;
    else
      return true;
}, "Can't be less than cost_of_car * 0.4");
于 2009-04-29T05:36:00.663 回答