我已经研究了一段时间了。我有一个 php 文件,它有一个 div,它读取一个目录并将目录中的文件加载到下拉列表中。当用户从文件中选择时,文件被选中并执行操作。但是,在刷新页面之前,文件名不会从下拉列表中消失。我已经尝试过“.load”但它不起作用。同样,div 的内容不会更新。
我的代码如下: 这是 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);?>
这是单击 div 时应重新加载所有内容的按钮:
$( "#vServer").on( "click", function(e) {
e.preventDefault();
$("#mgA").load(virtualDialog);
alert("refreshed");
virtualDialog.dialog( "open" );
});
如果有人有任何想法,请告诉我,我们将不胜感激!谢谢!
最佳答案
正如 epascarillo 所指出的,您错误地使用了 load()
。这可能更符合您希望执行的操作但我没有测试(甚至非常仔细地检查)您的 PHP 代码。
我还假设 div #mgA
是您使用 virtualDialog 打开的 jQueryUI 对话框的内容。
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
<?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;
?>
查看这些简单 AJAX 的附加示例——有时查看真正简单的示例会有所帮助:
关于javascript - 即使使用 'load',div 也不会重新加载更改的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287991/