doutongwei4380 2015-09-12 00:22
浏览 7
已采纳

按Id检查复选框

I am using the following piece of code to check some checkboxes based on their Id and a value stored in a array. So if the array has a value, the corresponding checkbox should become checked.

for($k=0;$k<count($array_ids);$k++){
     echo'<script type="text/javascript">

    var cb = document.getElementsById("id_'.$array_ids[$k].'");
    cb.checked = true;

</script>';
}

And this is the code for the checkboxes:

<input type="checkbox" id="id_'.$array_ids[$k].'" name="'.$array_ids[$k].'" value="checked" class= "My_class" onchange="add_on_textBox(this,\''.$array_ids[$k].'\',\'My_class\')">

And this is the var_dump result for the $array_ids:

array(2) {
  [0]=>
  string(5) "Name1"
  [1]=>
  string(5) "Name2"
}

The code doesn't work. I am pretty sure I am missing something.

  • 写回答

2条回答 默认 最新

  • dongmaopiao0901 2015-09-12 00:30
    关注

    Since ID's are unique in a page, there is no plural of getElementById().

    You are using getElementsById()...remove the s.

    This should be throwing errors in browser dev tools console also which would give you clue where the problem is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿