2012-06-02 37 views
0

我有相應行中指定數組文本框的複選框數組。如果用戶選擇特定的複選框,例如前三個複選框,我想驗證相應的文本框(空驗證)。我怎樣才能做到這一點?我已經嘗試了整個數組文本框,它工作正常。但是我只想驗證選中的選項。我給代碼供您參考:如何獲得選中數組複選框的數組文本框的驗證

dml=document.forms['form1']; 
// get the number of elements from the document 
len = dml.elements.length; 
for(i=0 ; i<len ; i++) 
{ 
    //check the textbox with the elements name 
    if (dml.elements[i].id=='noofdays[]') 
    { 
     // if exists do the validation and set the focus to the textbox 
     if (dml.elements[i].value=="") 
     { 
      alert("Please enter no of Days"); 
      dml.elements[i].focus(); 
      return false;  
     } 
    } 
} 

感謝您的快速回復。但在我的過程中。我從我的數據庫中獲取複選框的值作爲id。我已經給你的參考代碼。

<? while(($count<$rpp) && ($i<$tcount)) 
    { 
     mysql_data_seek($res,$i); 
     $_SESSION['cardid'][$i]=$row['id']; 
     $row = mysql_fetch_array($res); 
?> 

<tr> 
    <td align="center" bgcolor="#e9e9e9" class="running_text"><input name="id[]" type="checkbox" id="id[]" value="<?=$row['jobid']?>" onchange="enableTextField(this)" /></td> 
    <td height="28" align="center" bgcolor="#e9e9e9" class="running_text"><?=$row['jobtitle']?></td> 
    <td align="center" bgcolor="#e9e9e9" class="running_text"><?=$row['jobcategoryid']?></td> 
    <td align="center" bgcolor="#e9e9e9" class="running_text"><?=$row['practicename']?></td> 
    <td align="center" bgcolor="#e9e9e9" class="running_text"><?=$row['subscriptiontype']?></td> 
    <td width="82" align="center" bgcolor="#e9e9e9" class="running_text"><input type="text" size="1" value="0" name="noofdays[<?php echo $row['jobid']; ?>]" id="noofdays[]"> 
    < /td> 
    <td width="28" align="center" bgcolor="#e9e9e9" class="running_text"><a href="viewjobdetails.php?jobid=<?=$row['jobid']?>&ss=<? echo $_POST['noofdays'][$jobsid];?>"><img src="images/view-detail-icon.png" width="16" height="16" title="View" /></a></td> 
    <td width="31" align="center" bgcolor="#e9e9e9" class="running_text"><a onClick="return del(<?=$row['jobid']?>)" class="deleteimage"><img src="images/Delete-icon.png" width="16" height="16" title="Delete" /></a></td> 
</tr> 

<?  $i++; 
     $count++; 
    } ?> 

回答

0

給您的複選框輸入一個與您的文本輸入類名匹配的值。將您的 文本輸入與類名稱分組。

<input type="checkbox" value="lips"/> <--------Grouper 

<input type="text" class='lips' />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ <-Groupeee 
<input type="text" class='lips' /> <-Groupeee 
<input type="text" class='lips' />​ <-Groupee 

<script type="text/javascript"> 
    var ckBxs = document.querySelectorAll('input[type=checkbox]'), 
        i = ckBxs.length; 
    while (i--) { 
     ckBxs[i].onchange = checkBoxCheck; 
    }; 
    function checkBoxCheck() { 
     var txBxs = document.getElementsByClassName(this.value), 
         i = cxBxs.length; 
     while (i--) { 

      switch (txBxs[i].class) { 

       case 'lips': 
        if (txBxs[i].value != 'To what ever your checking for') 
        { alert('You\'re in focus Fool!'); }; 
        break; 
        // 

       case 'nextClassGroup': 
        if (txBxs[i].value != 'To what ever your checking for') 
        { alert('You\'re in focus Fool!'); }; 
        break; 
      }; 
     }; 
    }; 
</script> 
+0

感謝您的快速回復。但在我的過程中。我從我的數據庫中獲取複選框的值作爲id。我已經給你的參考代碼。 –

+0

朋友我稍微修改了我的問題,因爲我在前面的問題中沒有提供複選框和輸入框。請檢查以上內容 –

+0

如果您從數據庫中提取數據,您仍然可以像ID一樣添加類。如果你打算讓你的數據庫管理你的html,我會建議你添加這些類。你正在做的事情是使用1 *,「一對多」的關係,因爲如果一個給定的複選框管理多個文本框,那麼這與一對多相同。你可以在服務器上做所有的事情。 –