2012-04-17 64 views
0

可以使用jQuery來驗證這些表單輸入嗎?jQuery表單驗證,表單輸入的名稱是這樣的[]

<input type='text' name='amount[]' size='1' /> 
<input type='text' name='amount[]' size='1' /> 
<input type='text' name='amount[]' size='1' /> 

或者也許有一些選擇?

找到了解決辦法 - http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements/

+1

我會感到非常驚訝如果jQuery的[驗證](HTTP:/ /docs.jquery.com/Plugins/Validation)插件沒有處理這些,所以可能值得一試。 – 2012-04-17 14:22:42

+0

這種命名方式應該保留給單選按鈕組。你想達到什麼目的? – 2012-04-17 14:30:15

+0

我試圖製作購物車,使用這樣的名稱,因爲有不同數量的輸入,每次輸入1個輸入。 – user1093555 2012-04-17 14:33:05

回答

0

嘗試這個

<input type='text' name='amount[]' size='1' id="id1" /> 
    <input type='text' name='amount[]' size='1' id="id2"/> 
    <input type='text' name='amount[]' size='1' id="id3"/> 

,並使用ID驗證

0

嘗試:

​$("input[name='amount[]']").each(function(i, item){ 
    // Do your validation here.. 
    // You can use $(this) to refer to the element 
});​​​​​​​​ 
+0

Sry我是jquery的新手,我可以使用它有多暴露? Curently我得到這個代碼 'code' $(文件)。就緒(函數(){ $( 「#cartform」)驗證({ errorPlacement:功能(錯誤,元素){ error.appendTo(元.next()); } }); }); 'class ='required'/> – user1093555 2012-04-17 14:41:55

+0

你使用什麼插件? – 2012-04-17 14:46:37

+0

jquery.validate.js – user1093555 2012-04-17 14:48:40

0

使用索引在jquery的是這樣的:

$('input').eq(0).bind('change keyup',function(){ ... }); 

或使用以下對於第二輸入框的代碼:

$('input').eq(1).bind('change keyup',function(){ ... }); 

或這一個第三輸入框:

$('input').eq(2).bind('change keyup',function(){ ... });