2011-11-18 36 views
0

我需要驗證表單中的一組名稱,手機號碼字段。爲了方便使用服務器端腳本,我使用數組表示法命名了這些字段(即使用方括號)。 此外,下面的相同的html重複自己的次數取決於需要多少記錄。jquery驗證FIELDS相互依賴的一對

<table width="100%" border="0"> 
<tr><td class="b_name"><label>Name <sup>*</sup></label></td><td class="b_mobile"><label>Mobile <sup>*</sup></label></td><td class="removetext"><a href="javascript:void(0);" onclick="removebroadcast(this);" id="remove_<?php echo $cnt;?>">Remove</a></td></tr> 
<tr><td class="b_name" id="name_<?php echo $cnt;?>"><input type="text" value="" name="broadcast_name[]" class="broadcast_name" /></td><td class="b_mobile" id="mobile_<?php echo $cnt;?>"><input type="text" value="" name="broadcast_mobile[]" class="broadcast_num" /></td><td id="message_<?php echo $cnt;?>">&nbsp;</td></tr> 
</table> 

在規則/郵件的一部分,到目前爲止,我想出了:

'broadcast_name[]':{required:function(element){ 
        return $(element).parent().next().children('input').val()!=''; 
       } 
      }, 
'broadcast_mobile[]':{required:function(element){ 
        return $(element).parent().prev().children('input').val()!=''; 
       } 
      }, 

然而,這並不只針對第一套名稱/移動領域的工作。我也嘗試了一些其他的技巧(例如,使用$ .each類型驗證,使用自定義規則函數等),但他們都沒有工作。有什麼我在這裏做錯了嗎?請幫忙! :(

回答

-1

你可能想看看這個爲手機領域:http://digitalbush.com/projects/masked-input-plugin/

而且你不能這樣做

$('form').submit(function(e){ 
    var errors = false; 
    $(this).find('input').each(function(){ 
     if($(this).val() === ''){ 
      errors = true; 
     } 
    }); 
    if(errors){ 
     e.preventDefault(); 
     alert('errors exist'); 
    } 
}); 
+0

感謝面具插件,但我實際上已經使用它在手機領域,只是沒有想到有必要提供一切的詳細信息。 關於提供的解決方案 - 我需要在特定的情況下使用jQuery驗證插件,如果以前不明確,請致電 – zarun

+0

歡迎,我想知道爲什麼我的答案不適合你 –