【发布时间】:2012-02-28 17:48:39
【问题描述】:
这是无法正常工作的代码的 sn-p。由于某种原因,javascript 的结束 </script> 标记无法识别。 javascript 用于选中所有复选框,该复选框将选中或取消选中每个表单生成的所有复选框。每个表单都是独立的,所以我尝试通过包含在 php 循环中来精简我的代码。我有这个代码的一个版本工作,但它涉及多次重复相同的 javascript 示例。如果您能想出一种不同的方法,请随时改进代码。目前,尽管我需要弄清楚为什么 javascript 的结束标记不起作用。
foreach($words as $word){
$i++;
$qry = "Select * FROM subs WHERE type = '$word'";
$q = mysql_query($qry) or die(mysql_error());
?>
<script type="text/javascript">
checked=false;
function checkedAll<?=$i?>(frm<?=$i?>)
{
var aa= document.getElementById('frm<?=$i?>');
if(checked == false) {
checked = true
}
else{
checked = false
}
for(var i =0; i < aa.elements.length; i++) {
aa.elements[i].checked = checked;
}
}
</script>
<?
echo '<form action="attacher.php" method="POST" id="frm'.$i.'" >';
echo '<input type="hidden" name="template" value="16">';
echo "<table border='1' cellpadding='10'>";
echo "<tr><td colspan=4 align='center'><strong>$word</strong></td> <td><input type='submit' value='Add to Template'></td></tr>";
echo "<tr><th><input type='checkbox' name='checkall".$i."' onclick='checkedAll".$i."'(frm".$i.");'></th><th colspan=3> CONTROLS</th> <th>$word's</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array($q)) {
// echo out the contents of each row into table
echo "<tr>";
echo '<td><input type="checkbox" name="word[]" value="'.$row['id'].'"</td>';
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo '<td><a href="attacher.php?template=16&word=' . $row['id'] . '">Attach</a></td>';
echo '<td>' . $row['word'] . '</td>';
echo "</tr>";
}
// close table>
echo "</table><br>";
echo '</form>';
}
?>
【问题讨论】:
-
您的配置中是否允许使用 php 短标签?
-
什么是 JavaScript 标记?编辑:哦,你说的是 HTML
script标签? -
嗯,你为什么用
<script "text/javascript">而不是<script type="text/javascript">? -
删除 () 中的空格。顺便说一句,你为什么要在 php for 循环中回显一段 javascript?你可以做一些比这更好的事情,创建一个全局函数来切换表单中的每个复选框。
-
事实上,您完全可以使用
"text/javascript"。另外,我看到</script >中有一个拖尾空间。为了安全起见(尽管这不应该引起问题),我将其更改为</script>。
标签: php javascript loops syntax