我在href click上有下面的代码,我正在调用一个javascript代码,在这个代码中调用了一个ajax,它以json格式返回数组$ss的值。现在我想知道如何通过ajax更新$ss的值。
<div class="white" id="white" style="display:none">
<?php
foreach ($ss as $key => $value){
?>
<a href='javascript:void(0);' onclick='callAjax('<?php echo $key; ?>')'>
<?php
echo $value;
?>
</a>
<?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var res;
function on(id){
//alert('hi '+id);
$.ajax({
url: 'ajax.php', //This is the current doc
type: "GET",
data: ({a: id }),
success: function(data){
res = data;
//alert(res);
document.write(res);
}
});
}
</script>对于$ss,ajax.php文件返回数组值。我知道如何通过ajax更新普通div的数据,但在传递ajax调用返回的数据以更新数组值时遇到了问题。
发布于 2016-12-27 17:51:25
javascript不会替换变量的内容。-1-javascript
2-即使这样,你也不能以这种方式重新生成你需要的html。
html 3-收到变量后,需要更新。
PS:当然你可以在收到ajax调用时更新server端的变量,
这也需要一些要求(变量是全局的,并且可以在php文件中访问...),
但据我所知,你想用javascript来改变它,这是不可能的。
发布于 2016-12-27 17:56:27
你的ajax一定是这样的。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var res;
function on(id){
//alert('hi '+id);
$.ajax({
url: 'ajax.php', //This is the current doc
type: "GET",
data: {a: id },
success: function(data){
res = data;
//alert(res);
document.write(res);
}
});
}
</script>发布于 2016-12-27 17:57:30
我想你不能。
PHP代码在服务器端运行,jQuery在客户端运行。从jQuery更新PHP变量的方法是使用一个提交到PHP页面的jQuery调用,并让PHP查找它。
$ss = 'ss value';
if exists($_POST['ss']) {
$ss = $_POST['ss'];
}https://stackoverflow.com/questions/41342759
复制相似问题