javascript onload 在 while

标签 javascript php jquery html

我正在尝试使用 maps api autocomplete 初始化输入 - 输入的数量来自数据库 - ,但我无法在 while 循环中执行简单的 javascript 函数。

window.onload 的初始化工作正常,但我无法在这个 while 循环中执行函数...

控制台中未出现错误,1 个结果来自数据库。

$limit = $bdd->prepare('MySQL query');
$limit->execute();

 while ($city = $limit->fetch()) { 
       echo 'result'; ?>

       <script type="text/javascript">
              function initializeLimitCity() {
                      alert("Hello World");
              };
              initializeLimitCity();
      </script>

<?php
}

最佳答案

您不能多次声明相同的功能...将您的代码更改为:

<script type="text/javascript">
  function initializeLimitCity() {
    alert("Hello World");
  };
</script>

<?
$limit = $bdd->prepare('MySQL query');
$limit->execute();

while ($city = $limit->fetch()) { 
  echo 'result'; ?>

  <script type="text/javascript">
     initializeLimitCity();
  </script>

<?php
}
?>

关于javascript onload 在 while,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096824/

相关文章:

javascript - 在圆形路径周围放置不同大小的圆圈,没有间隙

javascript - 这在 javascript 中带有原型(prototype)

PHP - 使用登录系统保护仅限成员(member)的页面

javascript - 推迟 JavaScript 执行,直到内容添加到文档中

移动设备丢失 CSS 属性的 Jquery 下拉菜单

jquery - 是否可以在不同时间设置不同的select2参数?

javascript - 如何从服务器端 perl 程序获取图像数据并使用 javascript 在 div 中显示

javascript - 带有两个计数器的 For 循环,以及令人困惑的 if-else 语句。(JavaScript)

php - 有人可以帮忙解决 MYSQL 错误消息吗?

php - 在 PHP 中动态生成数组的适当引用