dongqi9125 2018-07-20 16:01
浏览 355

document.getElementById()。options.length显示从列表栏中选择的最后一个元素,而不是当前选择

I have a script that allows the selection of a directory from a drop down select and then displays its subdirectories in another drop down select. I am trying to check if the selected directory has sub directories, and if so, display a message indicating whether or not there are subdirectories available, ie, "no subdirs" or "subdirs".

The problem is that the alert message is displayed for the last element selected from the directories drop down, rather than the current directory selection.

For example, if the current selection is directory 2, and the previous selection was directory 1, document.getElementById("subdir").options.length; is shown for directory 1, rather than directory 2. I'm not sure, where the problem is, so any assistance would be greatly appreciated, thanks.

<script type='text/javascript'>
  $(function() {
    $("#dirs").on('change', function() {
      var Dir = $(this).val();
      //Make an ajax call 
      $("#subdir").html('<option>Loading...</option>');
      $.get("ajax.php?Dirs=" + encodeURIComponent(Dir), function(data) {
        $("#subdir").html(data);
      });
    });
  });
</script>
<script>
  function myFunction() {
    var y = document.getElementById("subdir").options.length;

    alert(y);

    if (y <= 1) {
      alert('no subdirs');
    } else if (y > 1) {
      alert('subdirs');
    }
  }
</script>
<select id="dirs" name="Dirs" onChange="myFunction()">
  <option value="" selected="selected" disabled="yes">Select Directory</option>
  <?php
     $dirs = glob("/*", GLOB_ONLYDIR);
     foreach($dirs as $val){
       echo '<option value="'.$val.'">'.basename($val)."</option>
";
     }
  ?>
</select>
<select name="subdir" id="subdir" required>
  <option value="Select Sub Directory" selected="selected" disabled="yes">Select Sub Directory</value>
</select>

And ajax.php:

<?php
/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

  //Check for passed dir exist 
  $folder = $_GET['Dirs'];

  if(!file_exists($folder)) {
    exit('Folder Not Found');
  }

  $out = '';

  $files = array_filter(glob($folder.'/*'), 'is_dir');
  echo '<option value="Select Sub Directory" selected="selected" disabled = "yes">Select Sub Directory</value>';

  foreach($files as $val){
    $out .= '<option value="'.$val.'">'.basename($val)."</option>
";
  }

  //Output the file options
  exit($out);
}
?>
  • 写回答

1条回答 默认 最新

  • donglaoping9702 2018-07-21 05:38
    关注

    Problem is simple; it all has to do with timing

    <select id="dirs" name="Dirs" onChange="myFunction()">
    

    See this? What it says is whenever you change this selectbox run a function named myFunction() immediately --> before anything else even before doing any ajax!

    function myFunction() {
        var y = document.getElementById("subdir").options.length;
    
        alert(y);
    
        if (y <= 1) {
        alert('no subdirs');
        }
        else if (y > 1) {
        alert('subdirs');
        }
    }
    

    See it fiddles with #subdir contents but ajax still isn't called yet, we are still with whatever content was there for earlier #dirs in short we have stale data in #subdir That's why it always shows you previous data

    How to fix? Just cut it from there and move the function call inside the ajax callback

    $( "#subdir" ).html( data ); 
     myFunction();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多