Javascript 生成行中的复选框

Javascript 生成行中的复选框,javascript,html,Javascript,Html,我有一个PHP网页,它在一个表中显示所有记录。我想对所有行添加复选框,用户可以选中复选框以选中每个生成行中的复选框 <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2"> </td> </tr> 我怎样才能做到这一点?您必须使用javascript。加: <script type="text/javascript"> var ch

我有一个PHP网页,它在一个表中显示所有记录。我想对所有行添加复选框,用户可以选中复选框以选中每个生成行中的复选框

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

我怎样才能做到这一点?

您必须使用javascript。加:

<script type="text/javascript">
var check=document.getElementById("The id of the checkbox"), 
table=document.getElementById("The id of the table");

check.onclick=function(){
var ck=table.getElementsByTagName("input");
for(i=0;i<ck.length;i++) if(ck[i].type=="checkbox") ck[i].checked=true;
}
</script>
        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

var check=document.getElementById(“复选框的id”),
table=document.getElementById(“表的id”);
check.onclick=function(){
var ck=table.getElementsByTagName(“输入”);

对于(i=0;i,我过去使用的一种方法是定义复选框id并使用它

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
这是我写的一个对我有用的样本

<?php
  $num_chkboxes = 3;
?>
<html>
  <head>
    <script type="text/javascript">
      function chk_all()
      {
        for (i = 1; i <= <?php echo $num_chkboxes; ?>; i++)
        {
          document.getElementById('chk'+i).checked = true;
        }
      }
    </script>
  </head>
  <body>
    check all <input type="checkbox" name="chkall" id="chkall" onclick="chk_all();" /><br /><br />

    <table>
      <tr>
        <td>
          chk1<input type="checkbox" name="chk1" id="chk1" />
        </td>
      </tr>
      <tr>
        <td>
          chk2<input type="checkbox" name="chk2" id="chk2" />
        </td>
      </tr>
      <tr>
        <td>
          chk3<input type="checkbox" name="chk3" id="chk3" />
        </td>
      </tr>
    </table>
  </body>
</html>
        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

函数chk_all()
{

对于(i=1;i我不能使用此代码

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
我的代码是这样的

  <table id="top" width=100% border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" style="border-style:solid; border-width:0.5px;">
  <form id="myForm" name="myForm" action="" method="post">
    <tr style="background-color:#C0C0C0;">          
      <td width="155" align="center"><b>Product Group</b></td>
      <td width="318" align="center"><b>Group Name</b></td>
      <td width="310" align="center"><b>PIC</b></td>          
      <td align="center" colspan="2">&nbsp;</td>  
      <td width="21" bordercolor="#000000" style="border-style:solid; border-width:1px;">
        <input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', true);" value="I like them all!">
        <input type="checkbox" id="checkall" name="ca_v1_on" value="Check All myCB" onclick="checkAll(1);"/>
          <input type="checkbox" name="ca_v1_off" value="Uncheck All myCB" onclick="checkAll(0);"/>
        <!-- ?php echo $form->checkbox('done'); ? -->
      </td>        
    </tr>
        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

产品组
组名
照片
echo$form->end(); foreach($prodGroupData作为$arr) { ?>
链接('Edit',array('controller'=>'productMasters','action'=>'editGroup','?'=>array('prodGroupNo'=>$arr['ProductGroup']['PROD\u GROUP\u NO']));?> 链接('Delete',数组('controller'=>'productMasters','action'=>'deleteGroup','?'=>array('prodGroupNo'=>$arr['prodGroupNo']));?>

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

页面[][>>] 链接('Delete Selected Items',array('controller'=>'productMasters','action'=>'deleteGroup','?'=>array('prodGroupNo'=>$arr['prodGroupNo']));?>

老实说,我正在做cakephp…我完全迷路了

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
以下是一个更清晰的版本:

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
创建('ProductGroup',array('url'=>array('controller'=>'productMasters','action'=>'addGroup')); ?> 在a中,在a中:参观{ 颜色:#0000FF; }
提交('Search',array('controller'=>'productMasters','action'=>'searchGroup'));?> 提交('Add',array('controller'=>'productMasters','action'=>'addGroup'));?> 产品组 组名 照片 复选框('checkAll',数组('onClick'=>'checkAll(1)');?> 链接('Edit',array('controller'=>'productMasters','action'=>'editGroup','?'=>array('prodGroupNo'=>$arr['ProductGroup']['PROD\u GROUP\u NO']));?> 链接('Delete',数组('controller'=>'productMasters','action'=>'deleteGroup','?'=>array('prodGroupNo'=>$arr['prodGroupNo']));?> 复选框('check');?--> 页面[][>>] 链接('Delete Selected Items',array('controller'=>'productMasters','action'=>'deleteGroup','?'=>array('prodGroupNo'=>$arr['prodGroupNo']));?>
end(); ?> 我使用的javascript是

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
函数CheckAll(标志) { 警报(document.getElementById('check').length); if(document.ProductGroupAddForm.check.length) { 对于(var x=0;x 位于不同文件(布局文件)中的

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>
整个代码是由php、html和cakephp混合而成的
这真的让我筋疲力尽,但我不想放弃…

这将起作用,但它将选中表单上的所有复选框。如果还有其他复选框,则需要比较name属性。不,您将只选中表中的复选框。但我同意您的看法,插入属性过滤器更好。仍然不起作用。我没有主意已经..另外,这种格式是违反规定的吗?我对你的问题感到困惑。你的问题是如何使用PHP生成复选框,还是如何一次选中所有复选框,或者同时选中所有复选框?我已经生成了带有复选框的行..但是我需要添加一个标题(全部选中)复选框我尝试了很多不同的方法,但都无法使事情顺利进行我真的不知道该怎么办出于好奇,为什么你的复选框在表格之外?不,它们不是。但是它们很远。(我的错)这就是为什么我要问这个格式是否有效的原因。复选框我是web开发新手,正在尝试自己学习。请帮助。表单应该在表声明之外,也就是说…从你上面的帖子中,我仍然不明白的是,你有echo$form->end();foreach(…{…}你确定在你所有的元素之前不会出现吗?
        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>