以下是一个ajax帖子页面,它在运行时呈现复选框。我在编写选择全部按钮的脚本时遇到问题,当我点击按钮时,只有1个值被选中而不是整个数组:
<?php
session_start();
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("../includes/functions.php");
if(isset($_REQUEST['t']))
{
$td = $_REQUEST['t'];
$t = split(",",$td);
$all = "";
$box_in_row = 0 ;
$this_box="<table border=0><tr>";
foreach($t as $table)
{
$this_box = "<td><h3>$table</h3>";
$result = mysql_query("SHOW FULL COLUMNS FROM $table FROM prfxcom1_prfx");
$options = "";
while($r = mysql_fetch_object($result))
{
if(!empty($r->Comment))
{
$options .= "<br><input type=checkbox name=\"".$table."[]\" value='$r->Field' id=\"$table\">" . $r->Field;
}
}
if($table == "transfer_req")
{
$options .= "<br><input type=checkbox name=\"".$table."[]\" value='Net Profit' id=\"$table\">NetProfit";
}
$this_box .= $options;
// Button
$click = "$('#$table').attr('checked', 'checked')";
$button = "<br /><input style='margin-top:10px;' type='button' name='$table_button' id='$table_button' value=' Select All ' onclick=\"$click\"/>";
$all .= "<div class='tblBox'>".$this_box.$button."</div></td>";
}
//$all = "<table class=\"listing form\" cellpadding=\"0\" cellspacing=\"0\">".$all."</table>";
echo $all;
}
?>
问题在于:
$click = "$('#$table').attr('checked', 'checked')";
请建议,我坚持这个。
谢谢, Hardik
答案 0 :(得分:0)
忽略代码中的许多问题并简单回答问题:
您需要使用类名而不是ID来引用复选框(您已经为它们提供了相同的ID)
对于这些行:$options .= "<br><input type=checkbox name=\"".$table."[]\" value='$r->Field' id=\"$table\">" . $r->Field;
更改为:$options .= "<br><input type=checkbox name='" . $table . "[]' value='" . $r->Field ."' class='" . $table . "'>" . $r->Field;
对于这一行:$click = "$('#$table').attr('checked', 'checked')";
使用单引号或转义$
更改为:$click = '$("."'.$table.'").attr("checked", "checked")';
答案 1 :(得分:0)
WHAT ???
$click = "$('#$table').attr('checked', 'checked')";
如何在PHP文件中编写Javascript?它需要在脚本标签中,但即使这样,PHP也会在服务器上运行,并且不会为您呈现Javascript。
添加script
代码,将您的ID更改为单独的ID并为其提供与tableClassName
相同的类,然后编写以下内容。
$(function(){
$('.tableClassName').attr('checked', 'checked')";
});