【问题标题】:Uncaught syntax error in dynamic jquery [closed]动态jquery中未捕获的语法错误[关闭]
【发布时间】: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 处理程序

标签: php jquery mysql pdo


【解决方案1】:

将此处末尾的逗号, 替换为$('#updateform".$row['id']."').serialize(),)

【讨论】:

    【解决方案2】:

    hide('slow') 后面缺少一个分号,serialize() 后面多了一个逗号

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多