【发布时间】:2016-03-21 14:15:50
【问题描述】:
关于这个主题有很多 Stack Overflow 问题,但似乎没有一个对我有用。
这可能是因为我需要在 div 刷新时调用 PHP 函数以更新信息。
我有一个#tab-project-CD-smt_test div,它显示了三个版本(生产、暂存和最新)。用户可以单击暂存或最新旁边的箭头以将其向上移动。我通过使用 AJAX 请求使这部分工作正常:
function sendToStaging (dir, type, subtype, version) {
//Need selected category, selected type, selected subtype
$.ajax({
type: 'POST',
url: 'configs.php',
data: 'function=sendToStaging'+'&dir='+dir+'&type='+type+'&subtype='+subtype+'&version='+version,
success: function() {
// window.location.reload(true);
$('#deployed').load(document.URL);
}
});
};
function sendToProduction (dir, type, subtype, version) {
//Need selected category, selected type, selected subtype
$.ajax({
type: 'POST',
url: 'configs.php',
data: 'function=sendToProduction'+'&dir='+dir+'&type='+type+'&subtype='+subtype+'&version='+version
});
如果成功,我想刷新在我的 PHP makeInfoSection 中创建的 #deployed div。摘录如下:
// list latest, staging, production
$html .= '<h4>Deployed Versions</h4>';
$html .= '<ul class="list-group" id="deployed">';
$html .= '<li class="list-group-item">';
$html .= '<span class="badge">production</span>';
$html .= $production.'</li>';
$html .= '<li class="list-group-item">';
$html .= '<span class="badge"><a href="#" style="color:orange"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction('."'$dir', '$type', '$subType', '$staging'".')"></span></a></span>';
$html .= '<span class="badge">staging</span>';
$html .= $staging.'</li>';
$html .= '<li class="list-group-item">';
$html .= '<span class="badge"><a href="#" style="color:orange"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToStaging('."'$dir', '$type', '$subType', '$latest'".')"></span></a></span>';
$html .= '<span class="badge">latest</span>';
$html .= $latest.'</li>';
$html .= '</ul>';
在 HTML 中调用该方法:
<div class="col-md-5 col-md-push-1 col-sm-6">
<div id="tabs" class="tab-content" style="padding:0em 0em 0em 1em">
<?php
foreach ($project_types as $type) {
// @TODO remove once folder structure is all setup
if ($type !== 'CD') continue;
foreach ($project_subtypes[$type] as $subType) {
$html = "<div role ='tabpanel' class='tab-pane'";
$html .= "id='tab-project-".$type."-".$subType."'>";
$html .= makeInfoSection($type, $subType, $project_versions[$subType], $project_dir);
$html .= "</div>";
echo $html;
}
}
foreach ($workflow_types as $type) {
foreach ($workflow_subtypes[$type] as $subType) {
$html = "<div role ='tabpanel' class='tab-pane'";
$html .= "id='tab-workflow-".$type."-".$subType."'>";
$html .= makeInfoSection($type, $subType, $workflow_versions[$subType], $workflow_dir);
$html .= "</div>";
echo $html;
}
}
?>
</div>
</div>
</div>
所以在 AJAX 成功后,我需要刷新它。我试过$('#tab-project-CD-smt_test').load(document.URL); 但这似乎没有任何作用。我也尝试过调用makeInfoSection 方法,但这并没有将它添加到 HTML 中,所以我对它不起作用并不感到惊讶:
$approved_functions = array('sendToProduction', 'sendToLatest', 'sendToStaging');
if(array_key_exists('function', $_POST) && in_array($_POST['function'], $approved_functions)) {
// call the approved function
$dir = $_POST['dir'];
$type = $_POST['type'];
$subType = $_POST['subtype'];
$version = $_POST['version'];
$_POST['function']($dir, $type, $subType, $version);
makeInfoSection($type, $subType, $versions, $dir);
}
关于如何让这个div 刷新有什么想法吗?
【问题讨论】:
-
我看到的一个问题是,您的 HTML 中至少会有两次相同的
iddeployed。makeInfoSection函数已将id='deployed'硬编码到其中。当您尝试更新特定 div 时,这将是一个问题,因为ids 应该是唯一的。 -
AJAX 加载当前页面以获取一些 PHP 呈现的 HTML 是相当常见的。我认为您与
.load函数很接近,但是当您调用它时,我相信它会尝试加载整个页面。根据它的文档,您可以尝试$('#deployed').load(document.URL + ' #deployed');这应该只采用#deployeddiv 并覆盖您 DOM 中当前#deployeddiv 的 innerHTML。 -
@Keeleon,这是真的,它被多次使用。它在 div
#tab-project-CD-smt_test下。所以我想我想刷新那个 div。 -
@tibsar 可以,是的。无论如何,它充当您的
deployeddiv 的父(容器)div。考虑为您部署的 div 使用一个类。 -
@romellem 请添加您的评论作为答案,并提供更多详细信息和示例代码,以便 OP 可以接受作为答案(或至少投票)
标签: javascript php jquery html ajax