dongyao2001 2014-08-21 09:45
浏览 37
已采纳

使用内联javascript更新下拉列表

I have a drop-down list implemented as follow:

   <select name="myDDownList" class="none" id="myDDownListID" style="height:25px; width:   280px">

   <?php
   if(file_exists($filesDir))
   {
     foreach (new DirectoryIterator($filesDir) as $file)
     {
       if((htmlentities($file) !== ".") && (htmlentities($file) !== ".."))
       {
         echo "<option>" .htmlentities($file). "</option>";
       }
     }
   ?>
   </select>

The content of the folder $filesDir can be updated automatically by the operating system, and I would like to update the content of the drop-down list automatically as well, without reloading manually the whole page content.

I though I can use an inline Javascript code using the setInterval function, changing the the code this way:

  <select name="myDDownList" class="none" id="myDDownListID" style="height:25px; width: 280px">

  <?php
  if(file_exists($filesDir))
  {
    foreach (new DirectoryIterator($filesDir) as $file)
    {
      if((htmlentities($file) !== ".") && (htmlentities($file) !== ".."))
      {
        echo "<option>" .htmlentities($file). "</option>";
      }
    }
  }
  ?>

  <script>
     /* use 'setInterval' function here */
  </script>
  </select>

But honestly I don't know how to update the content of the drop-down list inside the setInterval function. I think I have to use a function from the Javascript library. But I don't know which function.

Please, could you help me to find out a solution ? Thank you in advance for your help.

Simon

  • 写回答

1条回答 默认 最新

  • dpqvwgr74759 2014-08-21 10:08
    关注

    you make a php page "getListDirectory.php"

     <?php
      if(file_exists($filesDir))
      {
        foreach (new DirectoryIterator($filesDir) as $file)
        {
          if((htmlentities($file) !== ".") && (htmlentities($file) !== ".."))
          {
            echo "<option>" .htmlentities($file). "</option>";
          }
        }
      }
      ?>
    

    and your script simply make a request each 15 sec for istance:

    setInterval(function(){
    
    $.get( "getListDirecotry.php", function( data ) {
      $("#myDDownListID").html(data);
    });
    
    }, 15000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化