.load返回页面片段

时间:2016-01-19 06:30:35

标签: javascript php jquery

我试图从名为test2.php文件的PHP输出中加载数据并将其放入图表中

HTML:

<html>
    <body>
        <div class="ct-chart ct-perfect-fourth"></div>
        <div id="data"></div>
    </body>
</html>

jquery的:

$(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> contans <div id='number'>[the numbers]</div>如何让.load返回它加载到页面的相同内容

1 个答案:

答案 0 :(得分:1)

1)将id名称更改为numbers中的test2.php,因为您在ajax网址中使用test2.php #numbers

2)在$(this).text()回调函数中使用result代替.load()。它给出了id=data中加载的文本,在我们的例子中是数字。