php - document.friendform1.friends[i].checked 在复选框数组中只有一个元素时不起作用

标签 php javascript html

document.friendform1.friends[i].checked 在复选框数组中只有一个元素时不起作用。如果复选框数组中的元素不止一个,则它工作正常

我的html和php代码是

 $x=0;
foreach($result as $row)
{
    if($x==0)
    {?>
        <div class="invite-friends-con">
    <?php 
    }else{?>
        <div class="invite-friends-con" style="margin-left:70px;">
    <?php 
    }?>
    <div class="invite-friends-con-img">
       <img src="<?php echo base_url();?>Profile_images/<?php echo $row->vchPicture?>" width="48" height="39" align="absmiddle" />
    </div>
    <span><?php echo $row->vchFirstName?></span><input type="checkbox" name="friends[]"  style="margin-top:10px" value="<?php if($row->intFriendID==$this->session->userdata('user_id')){ echo $row->intMemberID;}else{ echo $row->intFriendID;}?>" id="friends"  />
    <input type="hidden" name="frnd[]" id="frnd" value="<?php echo $row->vchFirstName?>" />
    <?php if($x==1){$x=0;}else{$x++;}?>
<?php 
} ?>

javascript代码是:

 function addalfriend()
 {
   var str='';
   var str1='';

    var len=document.friendform1.friends.length;
    for(i=0;i<len;i++)
    {
      if(document.friendform1.friends[i].checked==true)
       {
         if(str=='')
         {
           str=document.friendform1.friends[i].value;
           str1=document.friendform1.frnd[i].value;
         }
         else
         {
           str=str+","+document.friendform1.friends[i].value;
           str1=str1+","+document.friendform1.frnd[i].value;
         }
       }
    }
        document.getElementById('invite_1').value=str;
        if(str!='' && str1!=''){
            document.getElementById('invited').style.display="block";
        }
        else{
            document.getElementById('invited').style.display="none";
        }
        document.getElementById('invited').value = str1;
        document.getElementById("low-high-div").style.display="none";
        document.getElementById("events-input-bg2").value="";
        document.getElementById("events-input-bg3").value="";
        closemessage1();
        return false;

 }

所以请有人告诉我我的错误。

谢谢

最佳答案

当有一项返回时,则不返回数组,返回元素对象,因此您需要以不同的方式访问它 作为

var len=document.friendform1.friends.length;
if(len>1)
for(i=0;i<len;i++)
{
  if(document.friendform1.friends[i].checked==true)
   {
     if(str=='')
     {
       str=document.friendform1.friends[i].value;
       str1=document.friendform1.frnd[i].value;
     }
.
.
.
}
else
{
  if(document.friendform1.friends.checked==true)
   {
     if(str=='')
     {
       str=document.friendform1.friends.value;
       str1=document.friendform1.frnd.value;
     }
   }
.
.
.

}

关于php - document.friendform1.friends[i].checked 在复选框数组中只有一个元素时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15379256/

相关文章:

php - 有没有办法为同一个表组合多个连接?用于 V 4.6 的 WP 元查询

javascript - Div 将自身的一半显示两次 - 使用 swipe.js

jquery - div创建的随机位置

javascript - 为什么在选择列表上拖动会更改或清除选择?

php - 从数据库表输出csv文件

javascript - 将我的数据库中可用的 pdf 上传到我的服务器

php - fatal error mysql PHP

javascript - 无法使用 axios 发送带 header 的 get 请求

javascript - 如何将 ruby​​ 嵌入到 JavaScript 中

javascript - 是否可以输入 CSS 边框