如何在选中数组复选框的情况下获取数组文本框的验证

How to get the validation for an array textbox with the array checkbox selected

本文关键字:数组 情况下 获取 文本 验证 复选框      更新时间:2023-09-26

我有一组复选框,相应的行中有指定的数组文本框。如果用户选中特定的复选框,例如前三个复选框,我想验证相应的文本框(空验证)。我该怎么做?我已经尝试了整个数组文本框,它工作正常。但我只想验证已检查的。我给出代码供您参考:

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++; 
    } ?>

为复选框指定一个与文本输入的类名匹配的值。分组您的带有类名的文本输入。

<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>