dream518518518 2014-07-27 16:23
浏览 45
已采纳

如何从复选框中将名称放在JavaScript getElementById函数中

So, I've got this code:

PHP

if($row['accept']==1){
    echo "<input type=\"checkbox\"  name=\"check[".$row['id']."]\" value=\"0\">Decline";
}
else{
    echo "<input type=\"checkbox\" name=\"check[".$row['id']."]\" value=\"1\">Accept";
}

JavaScript

function do_this(c){    
        var checkboxes = document.getElementsByName('check[]');
        var checkAll = document.getElementById('checkAll');

        var v=gid(c);
        if(checkAll.value == 'select'){
            for (var i in checkboxes){
                checkboxes[i].checked = '';
            v.style.display="none";
        }
            checkAll.value = 'deselect';
        }else{
            for (var i in checkboxes){
                checkboxes[i].checked = 'true';
            v.style.display="inline";
        }
            checkAll.value = 'select';
            }
}

I need to get the name from the input checkboxes, and get it with the document.getElementsByName('the name needs to be here'). But I don't know how to write the name form the checkbox in the Java function. Is there a posibility to do this? Thank you in advance!

  • 写回答

2条回答 默认 最新

  • doutan3371 2014-07-27 16:38
    关注

    you could get all checkboxes, using:

    var cBoxEles = document.querySelectorAll('input[type="checkbox"])');
    //and loop thru it
    for (var i=0, len = cBoxEles.length; i < len; i++){
        console.log(cBoxEles[i].name);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看