Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 加载()JSON数据,生成HTML表并将其放入div中_Jquery_Jquery Load - Fatal编程技术网

Jquery 加载()JSON数据,生成HTML表并将其放入div中

Jquery 加载()JSON数据,生成HTML表并将其放入div中,jquery,jquery-load,Jquery,Jquery Load,我可能有一个非常简单的jQuery问题——可能遗漏了一点 我有一个按钮,可以从PHP脚本加载JSON格式的数据: <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> function showLastGame(id) {

我可能有一个非常简单的jQuery问题——可能遗漏了一点

我有一个按钮,可以从PHP脚本加载JSON格式的数据:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function showLastGame(id) {
        $('#top').load('http://preferans.de/cards-json.php?id=' + id + '&limit=1');
}
</script>
请问怎么做

以及如何将生成的字符串放入
#top
div?

使用常规调用而不是
.load()


我假设
renderGame
函数返回
#top
元素的HTML内容;如果没有,则将其更改为正确的函数调用。

我认为一种方法是使用

例如,基于您的代码:

function showLastGame(id) {
    $.getJSON('/cards-json.php?id=' + id + '&limit=1', function(data) {
        var html = '<ul>';
        $.each(data, function(key, val) {
            // Whatever you what to do, eg.
            html = html + '<li>' + key + ': ' + val + '</li'>);
        });
        html = html + '</ul>';
        $('#top').html(html);
    });
}
函数showLastGame(id){
$.getJSON('/cards json.php?id='+id+'&limit=1',函数(数据){
var html='
    '; $。每个(数据、函数(键、值){ //不管你做什么。 html=html+'
  • '+key+':'+val+'
    
    函数renderGame(cardsTable、nTr、html){//添加了要传递的html,以便可以进一步操作
    //…此处构造的HTML表。。。。
    //使用诸如'var part=$('selector',HTML_STRING)`之类的东西,您可以提取部分
    //修改,并使用它们为表生成字符串
    var part1=$('span.menu',html.eq(0)//从传递的html字符串中获取类为'menu'的第一个'span',并存储在'part1'中`
    等
    str=“”+第1部分+”“//等等。。。
    返回str;
    }
    函数showLastGame(id){
    //在获取数据后使用`$.get`和回调函数
    $.get('/cards json.php?id='+id+'&limit=1',函数(d){
    //d是包含“get”请求响应的字符串
    
    var table=renderGame(??,?)//什么是
    cardsTable
    nTr
    以及它们来自何处?请不要介意-这是我自己的函数-我在上使用它,并希望重用它。我的问题更多:如何在
    #top
    中放入字符串以及如何将加载的数据传递给函数。
    function renderGame(cardsTable, nTr) {
            var aData   = cardsTable.fnGetData(nTr);
            //console.dir(aData);
            // .... HTML table constructed here....
            return str;
    }
    
    $.ajax({
        url: '/cards-json.php',
        data: {
            id: id,
            limit: 1
        },
        dataType: 'json',
        ...
    }).done(function(data) {
        // data is your JSON - use it however you want here
        var topHtml = renderGame(arg1, arg2);
        $('#top').html(topHtml);
    });
    
    function showLastGame(id) {
        $.getJSON('/cards-json.php?id=' + id + '&limit=1', function(data) {
            var html = '<ul>';
            $.each(data, function(key, val) {
                // Whatever you what to do, eg.
                html = html + '<li>' + key + ': ' + val + '</li'>);
            });
            html = html + '</ul>';
            $('#top').html(html);
        });
    }
    
    <script type="text/javascript">
    function renderGame(cardsTable, nTr, html) { // added html to be passed so it can be manipulated further
    
            // .... HTML table constructed here....
            // using things like `var part = $('selector',HTML_STRING)` you can extract parts
            // to modify, and build a string for the table with them
    
            var part1 = $('span.menu',html).eq(0) // get the first `span` with class `menu` from the html string passed and store in `part1`
            ... etc ...
    
            str = "<table><tr><td>" + part1 + "</td></tr></table>" // etc...
    
            return str;
    }
    function showLastGame(id) {
        // use `$.get` and a callback function after it got data
        $.get('/cards-json.php?id=' + id + '&limit=1',function(d){
            // d is a string containing the response of the `get` request
            var table = renderGame(???,???) // <~ not sure what variables your function takes
            // set the HTML of the target element
            $('#top').html(table);
        });
    }
    </script>