2013-04-12 19 views
0

jQuery驗證依賴性表達我有這樣的片段:與數字字段

<input type="checkbox" name="sal_cart" id="sal_cart"> 
<input type="text" name="sal_unit_price" id="sal_unit_price" class="required"> 

sal_unit_price可以是數字或字符串,但它總是要求(不能爲空)。
到目前爲止這麼好。
如果sal_cart被檢查,那麼sal_unit_price必須是數字(字符串不再允許)
根據jQuery Validation dependency-expression,我把這個:

$('#form1').validate({ 
    rules: { 
     sal_unit_price: { number: '#sal_cart:checked' }, 
    }, 
    messages: { 
     sal_unit_price: { number: 'Please fill in a number' }, 
    } 
}); 

,但它不工作:即使checkbox沒有被選中,它仍然說它需要一個數字
請問,你能發現這有什麼問題嗎?

這裏是一個小提琴:http://jsfiddle.net/cWD8j/

回答

1

你使用的語法僅適用於需要規則,您需要一個較長的語法的其他規則,通過在物體PARAM取決於性能

updated fiddle

$('#form1').validate({ 
    debug: true, 
    rules: { 
     sal_unit_price: { 
      number: { 
       param : true, 
       depends: '#sal_cart:checked' 
      } 
     }, 
    }, 
    messages: { 
     sal_unit_price: { number: 'Please fill in a number' }, 
    } 
}); 
+0

謝謝,這個作品非常好!請問,你能指出我在哪裏可以找到這個在線文檔? – Ivan

+0

我不認爲這是記錄,雖然這裏使用依賴功能的http://khaidoan.wikidot.com/jquery-validation –

+0

你撥弄代碼是不工作的例子 –