【发布时间】:2013-01-22 08:48:17
【问题描述】:
我一直在编写一段动态 JavaScript 代码:
$(document).ready(function() {
<?php
$sql = "SELECT * from pm_schedule";
$result = $pdo->query($sql);
foreach ($result as $row)
{
echo
"$('#updatebtn".$row['id']."').click(function() {
$('#result".$row['id']."').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform".$row['id']."').serialize(),
});";
}
?>
});
当我在我的服务器上运行它时,它会扩展为以下内容:
$(document).ready(function () {
$('#updatebtn1').click(function () {
$('#result1').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform1').serialize(),
});
$('#updatebtn2').click(function () {
$('#result2').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform2').serialize(),
});
$('#updatebtn3').click(function () {
$('#result3').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform3').serialize(),
});
$('#updatebtn4').click(function () {
$('#result4').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform4').serialize(),
});
$('#updatebtn5').click(function () {
$('#result5').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform5').serialize(),
});
});
Google Chrome 不断为上述代码返回未捕获的语法错误 - 知道为什么吗?请注意,我必须对第一个代码块而不是第二个代码块进行编辑。
【问题讨论】:
-
我会说这是每个
serialize()和缺少的)之后的逗号 -
为什么所有的代码都是重复的?可以通过使用类而不是 ID 合并到一个
click处理程序