我正在尝试从名为 test2.php 的 PHP 文件的输出中加载数据并将其放入图表中
HTML:
<html>
<body>
<div class="ct-chart ct-perfect-fourth"></div>
<div id="data"></div>
</body>
</html>
j查询:
$(document).ready(function(){
$("#data").load("test2.php #numbers ", function(result){
console.log(result);
var data = result.split(",").slice(1);
var last_element = data[data.length - 1];
new Chartist.Line('.ct-chart', {
labels: data,
series: [data]
}, {
fullWidth: true,
height: 650,
chartPadding: {
right: 0
}
});
});
});
图形显示最后一个数字为 0 和 console.log(result);
返回:
<div id='number'>,-0.05,-0.07,-0.07,-0.07,0.14,0.14,0.09,0.07,0.07,0.07,0.07,0.65,0.63,0.63,0.63,0.63,0.63,0.58,0.56,0.56,0.56,0.56,0.84,0.79,0.77,0.77</div>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pokerHand[]" id="pokerHand" multiple="multiple" />
<input type="submit" id="submit" value="submit" name="submit">
<input type="hidden" name="test" value="abc" id="test" />
</form>
</body>
</html>
然而这是我的 php 文件的输出 <div id="data"></div>
包含 <div id='number'>[the numbers]</div>
我如何让 .load 返回它加载到页面的相同内容
最佳答案
1) 在 test2.php
中将 id
名称更改为 numbers
因为你使用了 test2.php #numbers
在 ajax 网址中。
2) 在.load()
回调函数中使用$(this).text()
代替result
。它给出了加载到 id=data
中的文本,在我们的例子中是数字。
关于javascript - .load返回页面片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869926/