0

当我使用 ajax 在页面上重新加载 div 时,它可以工作,但当我尝试重新加载脚本时却不行。我的 javascript 是动态的,来自数据库,我的 div 也是如此。这是我的代码:

        <ul id="classes">
            <?php
            $email = 'thomas990428@me.com_classes';

            $result = mysqli_query($con,"SELECT * FROM `$email` ORDER BY period"); if (!$result) echo mysqli_error(); else // ok, do your thing.

            $classcount = 1;

            while($row = mysqli_fetch_array($result))
              {

              $period = $row['period'];
              $teacher = $row['teacher'];
              $subject = $row['subject'];
              $subjecto = strtolower($subject);
              $subjecto = str_replace(' ', '', $subjecto);
              $grade = $row['grade'];

              echo "<li id='button" . $classcount . "' class='button' onclick='" . $subjecto . "(),homework" . $classcount . "()'><div class='deletebutton' onclick='deleteclass" . $subjecto . "()'>X</div><span class='course'>" . $classcount . ". " . $subject . "</span></li>\n";

              $classcount += 1;

              }

            ?>
    <script>

        <?php
        $email = 'thomas990428@me.com_classes';

        $result = mysqli_query($con,"SELECT * FROM `$email`");

        while($row = mysqli_fetch_array($result))
          {

          $period = $row['period'];
          $teacher = $row['teacher'];
          $subject = $row['subject'];
          $subjecto = strtolower($subject);
          $subjecto = str_replace(' ', '', $subjecto);
          $grade = $row['grade'];

          echo "function deleteclass" . $subjecto . "() { \n";
          echo "var hi = confirm('Are you absolutely sure you want to permantly delete this class? This cannot be undone.'); \n";
          echo "if (hi==true) \n";
          echo "{ \n";
          echo "document.getElementById('hiddenframe').src='deleteclass.php?class=".$subjecto."';";
          echo "closeedit();";
          echo "refreshclasses();";
          echo "} \n";
          echo "else \n";
          echo "{} \n";
          echo "} \n";

          }

        ?>


    </script>
        </ul>
        <iframe id="hiddenframe"></iframe>

所以基本上它会刷新 ul 而不是脚本。我需要它来刷新两者,以便做我需要它做的事情。有任何想法吗?谢谢!

4

1 回答 1

0

先看评论,然后...

在检查你的代码时,你为什么不这样做呢?

function deleteClass(subjecto, period, teacher, ...)

onclick='deleteClass(" . $subjecto . ", " . $period. ", " . $teacher. ")'

如果你现在只想让它工作,你可以这样做:

代替:

<div class='deletebutton' onclick='deleteclass" . $subjecto . "()'>X</div>

至:

<div class='deletebutton' onclick='deleteclass(" . $subjecto . ")'>X</div>

并将整个 echo <script> 部分替换为:

function deleteclass(subjecto){
    var hi = confirm('Are you absolutely sure you want to permantly delete this class? This cannot be undone.');
    if (hi==true)
    {
        document.getElementById('hiddenframe').src='deleteclass.php?class='+subjecto;
        closeedit();
        refreshclasses();
    }
}
</script>
于 2013-03-10T08:51:42.260 回答