2010-07-17 115 views
0

我有一大堆的领域像飞这样创建jQuery的验证插件 - 跨多个输入验证规则字段

<input type="text" name="coeff_a"> 
<input type="text" name="coeff_b"> 
<input type="text" name="coeff_c"> .. and so on .. 

我想验证输入到上述领域,并使用jQuery验证插件是。我已经建立了一个规则,像这样

jQuery.validator.addMethod(
    "perc", 
    function(value, element) { 
     // all the fields that start with 'coeff' 
     var coeff = $("input[name^='coeff']"); 
     var total; 

     for (var i = 0; i < coeff.length; i++) { 
      total += coeff[i].value; 
     } 

     if (total <= 100) { 
      return true; 
     } 

     return false; 
    }, 
    jQuery.format("Please ensure percentages don't add up to more than 100") 
); 

不用说,上述不工作。任何想法我做错了什么?

回答

1

您的函数没有返回正确的值。

function cals(value, element) { 
    // all the fields that start with 'coeff' 
    var coeff = $("input[name^='coeff']"); 
    var total = 0; 
    for (var i = 0; i < coeff.length; i++) { 
     total += Number(coeff[i].value); 
    } 

    if (total <= 100) { 
     return true; 
    } 

    return false; 
} 

编辑了一些东西。看一看。

+0

是的!做到了!谢谢。 – punkish 2010-07-17 13:09:07