javascript
  • jquery
  • 2012-04-30 222 views 0 likes 
    0

    您好IM來驗證我的形式。 我想添加一個更多的規則來驗證,但不能讓它工作。 輸入值應小於或等於前一個元素的值。添加驗證方法「jQuery的驗證插件」使用jQuery驗證插件

    這是可能做到的呢?

    這裏是代碼 -

    <input type='text' value='<?php echo $count; ?>' class='input_in_stock' readonly="readonly" /> 
    <input type='text' name='amount[]' id='<?php echo rand(0,99).$id; ?>' size='1' value='<?php echo $count; ?>' class='required input_count' /> 
    
    $(document).ready(function(){ 
    
        $.validator.addMethod('positiveint', function (value) { 
         return (value > 0) && (value != 0) && (value == parseInt(value, 10)); 
        }, 'Enter a positive integer value.'); 
    
        $.validator.addMethod('notmorethan', function (value) { 
         return (value <= $(this).prev().val()); 
        }, 'abc.'); 
    
    
    
    
        $("#cartform").validate({ 
         rules: { 
          "amount[]": { 
           required: true, 
           positiveint: true, 
           notmorethan: true 
          } 
         }, 
         errorPlacement: function(error, element) { 
          error.appendTo(element.next()); 
         } 
        }); 
    
    }); 
    

    附:我用這個jQuery驗證插件修復,使其與名這樣的工作[] http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements

    編輯:找到了解決辦法

    $.validator.addMethod('notmorethan', function (value, element) { 
        return (value <= $(element).prev().val()); 
    }, 'abc'); 
    
    +0

    請發表您的解決方案作爲一個答案,並接受它。 – Sparky

    回答

    1

    看看這個小提琴helps- http://jsfiddle.net/alokswain/LSuMA/11/。我不確定您使用的名稱如amount[]的自定義設置,但我認爲這種方法應該非常有用。

    自定義的驗證方法只是功能,如果使用this函數體內的話,那就指函數的所有者。現在,假設你想從輸入字段以前的有效價值,你可以隨時在整個表單有效的DOM一些地方保存它,說以前存儲的值被稱爲previousAmountVal。當字段中的值發生變化,在功能notmorethan您可以訪問使用value參數和以前存儲的值,並使用previousAmountVal於前值的電流值,從而決定電流值是否有效。另外,如果您這樣做,請記住在表單有效時始終更新previousAmountVal

    +0

    已經找到了我自己的解決方案,但無論如何thx幫助! – user1093555

    0

    找到了解決辦法

    $.validator.addMethod('notmorethan', function (value, element) { 
        return (value <= $(element).prev().val()); 
    }, 'abc'); 
    
    相關問題