私は動的な 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 は、上記のコードに対してキャッチされていない構文エラーを返し続けます - 理由について何か考えはありますか? 2 番目のコード ブロックではなく、最初のコード ブロックを編集する必要があることに注意してください。