2012-04-23 78 views
0

我有一个HTML表我已经更新到使用复选框可以删除多个文件:进程隐藏字段,并计算

<table> 
    <thead> 
    <tr> 
     <th>Camera Name</th> 
     <th>Date Created</th> 
     <th>Video Size</th> 
     <th>Video Length</th> 
     <th> 
      <button type="submit" class="deletebutton" name="delete_video" value="Delete" title="Delete the selected videos" onClick="return confirm('Are you sure you want to delete?')">Delete</button><br> 
      <input type="checkbox" name="radioselectall" title="Select All" /> 
     </th> 
    </tr> 
    </thead> 
    <tbody> 

<?php 

for($i=0;$i<$num_videos;$i++) 
{ 
     //do stuff 
     //Note: I'm looping here to build the table from the server 
?>    
     <tr > 
      <td onclick="DoNav('<?php echo $url; ?>');"> 
         <?php echo $result_videos[$i]["camera_name"]; ?> 
      </td> 
      <td onclick="DoNav('<?php echo $url; ?>');"> 
         <?php echo setlocalTime($result_videos[$i]["video_datetime"]); ?> 
      </td> 
      <td onclick="DoNav('<?php echo $url; ?>');"> 
         <?php echo ByteSize($result_videos[$i]["video_size"]); ?> 
      </td> 
      <td onclick="DoNav('<?php echo $url; ?>');"> 
       <?php echo strTime($result_videos[$i]["video_length"]); ?> 
      </td> 
      <td> 
       <form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST"> 
       <input type="checkbox" name="radioselect" title="Mark this video for deletion"/> 
       <input type="hidden" name="video_name" value="<?php echo $result_videos[$i]["video_name"]; ?>" /> 
       </form> 
      </td> 
     </tr> 

我开始先创建一些jQuery代码的所有创建一个选择/取消选择表格标题中的所有按钮,只是显示一个测试我可以找到哪些框被选中。这一切正常:

//selectall checkboxes - select all or deselect all if top checkbox is marked in table header 
$("input[name='radioselectall']").change(function() 
{ 
    if($(this).is(':checked')) 
    { 
     $("input[type='checkbox']","td").attr('checked',true); 
    } 
    else 
    { 
     $("input[type='checkbox']","td").attr('checked',false); 
    } 
}); 

//process checkboxes - which ones are on 
$(".deletebutton").click(function() { 
    $("input[name='radioselect']:checked").each(function(i){ 
     alert(this.value); 
    }); 
}); 

所以我卡住的部分是我不知道从哪里走。我需要通过所有选中视频的所有视频名称(使用复选框)。 video_name是我表单中隐藏字段的一部分。所以当我选择删除按钮时,我需要将它传递给我的php函数。不确定如何解决这个问题。希望这是有道理的。

回答

0

简单的解决方案可能:

更改复选框有值为1和$ result_videos [$ i] [ “VIDEO_NAME”]的名称;在表格行中,使表格封装整个表格。

然后在提交你可以这样做:

foreach ($_POST as $key => $value) { 
    //Delete $key (the name) where $value == 1 
} 
+0

其实不知道我明白吗? _POST只需要返回选中的复选框。 JavaScript代码知道什么是检查,但不是服务器代码? – Tom 2012-04-24 13:14:07

+0

如果您给复选框输入'$ result_videos [$ i] [“video_name”];那么这些将作为这些表单元素的POST数组中的键,给它们值1,然后检查这个告诉你他们是否在提交时被选中。 – Ing 2012-04-24 14:45:55

+0

哦,我明白了。我的问题是,由于布局原因,我在表单外创建了按钮。这意味着我需要在JavaScript中创建表单。我应该重做这个问题。感谢您的提示,因为可以使用复选框处理。我会标记为已解决。 – Tom 2012-04-24 18:56:30

0

你的做法是好的,如果你想在一个给定的点来访问通过jQuery的隐藏字段,但一旦你提交表单,并失去了DOM中,由于没有一致的命名方案,PHP处理页面将无法将选定的复选框与隐藏字段相关联。

一种方法是使用循环计数器的后缀两个以配对起来:

<input type="checkbox" id="radioselect_<?= $i ?>" title="Mark this video for deletion" value="1" /> 
<input type="hidden" id="video_name_<?= $i ?>" value="<?php echo $result_videos[$i]["video_name"]; ?>" /> 

如果要关联比这两个领域更多这将是一件好事。否则,您可以使用video_name作为复选框的值,如Ing所示。