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

1 回答 1

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>
于 2012-06-02T17:38:53.400 回答