简体   繁体   English

即使使用'load',div也不会重新加载更改的内容

[英]div does not reload the changed content even with 'load'

I have been working on this for some time now. 我已经有一段时间了。 I have a php file that has a div which reads a directory and loads the files in the directory into a dropdown. 我有一个php文件,它有一个读取目录的div,并将目录中的文件加载到下拉列表中。 When the user selects from the file the file gets selected and teh operation is performed. 当用户从文件中选择文件被选中并执行操作时。 However, the file name does not disappear from the dropdown until the page is refreshed. 但是,在刷新页面之前,文件名不会从下拉列表中消失。 I have tried '.load' and it doesn't work. 我试过'.load'但它不起作用。 As in, the content of the div does not get updated. 同样,div的内容不会更新。

My code is as follows: This is the PHP file: 我的代码如下:这是PHP文件:

<div id="mgA" class="form-group">
<label>Management Address:</label>
 <?php
   $path = "/licenses/ve/address/unused";
   clearstatcache();
  $files = array();
  $handle = opendir($path);
  echo '<select required id="mgmtAdd" class="form-control select2"      style="width: 100%;">';
  while ($file = readdir($handle)) {
 if (substr($file,0,1) != ".") {
 $files[]=$file;
  }
 echo "<option selected = 'selected' value='0'>Select</option>";
 natsort($files); //sorting
 foreach($files as $file){
 echo "<option value ='$file'>$file</option>";
 }
 echo '</select>';
   if (is_dir_empty($path)) {
    echo "Max no of hosts already created";
    }

function is_dir_empty($path) {
 if (!is_readable($path)) return NULL;
     return (count(scandir($path)) == 2);
  }
closedir($handle);?>

This is the button which on click the div should reload all the contents again: 这是单击div时应该再次重新加载所有内容的按钮:

   $( "#vServer").on( "click", function(e) {
    e.preventDefault();

    $("#mgA").load(virtualDialog);

    alert("refreshed");

    virtualDialog.dialog( "open" );
    });

Please let me know if anyone has any idea, any help is appreciated! 如果有人有任何想法,请告诉我,任何帮助表示赞赏! Thank you! 谢谢!

As epascarillo has pointed out, you are using load() incorrectly. 正如epascarillo指出的那样,你正在错误地使用load() This may be more in line with what you wish to do but I did not test (or even check very closely) your PHP code. 这可能更符合您的要求, 但我没有测试(甚至非常仔细地检查)您的PHP代码。

I am also assuming that the div #mgA is the content of the jQueryUI dialog that you are opening with virtualDialog. 我还假设div #mgA是您使用virtualDialog打开的jQueryUI对话框的内容。

javascript/jQuery: 使用Javascript / jQuery的:

$( "#vServer").on( "click", function(e) {
    e.preventDefault();
    $.ajax({
        type: 'post',
         url: 'ajax-reload.php',
        success: function(d){
            $("#mgA").html(d);
            virtualDialog.dialog( "open" );
        }
    });
});

ajax-reload.php Ajax的reload.php

<?php
    $path = "/licenses/ve/address/unused";
    clearstatcache();
    $files = array();
    $handle = opendir($path);
    $out = '<select required id="mgmtAdd" class="form-control select2"      style="width: 100%;">';
    while ($file = readdir($handle)) {
        if (substr($file,0,1) != ".") {
            $files[]=$file;
        }
        $out .= "<option selected = 'selected' value='0'>Select</option>";
        natsort($files); //sorting
        foreach($files as $file){
            echo "<option value ='$file'>$file</option>";
        }
    } //<=== this brace was missing
    $out .= '</select>';
    if (is_dir_empty($path)) {
        $out = "Max no of hosts already created";
    }

    function is_dir_empty($path) {
        if (!is_readable($path)) return NULL;
        return (count(scandir($path)) == 2);
    }
    closedir($handle);
    echo $out;
?>

See these additional examples of simple AJAX -- sometimes it helps to see the really simple examples: 查看简单AJAX的这些附加示例 - 有时查看非常简单的示例会有所帮助:

AJAX request callback using jQuery 使用jQuery的AJAX请求回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM