【问题标题】:How to get the validation for an array textbox with the array checkbox selected如何在选中数组复选框的情况下获取数组文本框的验证
【发布时间】:2012-06-02 16:29:01
【问题描述】:

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

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

【问题讨论】:

    标签: javascript arrays validation


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的快速回复。但在我的过程中。我从我的数据库中获取复选框值作为 id。我已将我的代码提供给您参考。
    • 朋友我稍微修改了我的问题,因为我没有在前面的问题中给出复选框和输入框。请在上面查看
    • 如果您从数据库中提取,您仍然可以像 id 一样添加类。如果您要让数据库管理您的 html,我建议您让它添加类。无论如何,您正在做的是使用 1*,“一对多”关系,因为如果给定的复选框管理多个文本框,那么这与一对多相同。您可以在服务器上完成所有这些操作。
    • 我无法理解您的回复朋友。请回复并与我上面的编码完全相同的代码示例
    猜你喜欢
    • 2020-06-26
    • 2021-10-07
    • 2016-06-24
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多