1

我不确定我要去哪里错了。

当用户单击不同的项目数量时,我正在尝试更改数据表。

我可能错误地使用了 $.each() 方法,因为我在页面上没有看到任何结果,而且我是一个完整的 jQuery 菜鸟。感谢您的帮助,谢谢

测试表.html

<!DOCTYPE html>
    <html>
    <head>
    <title>test</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

        function swapContent(count){
            $(".price-sidebar").html("Put animation here").show();

            $.ajax({
                type: "POST",
                dataType: 'json'
                url: "myphpscript.php",
                data: {countVar: count},
                success: function(data){
                    $('#content').empty();

                    $.each(data, function(){
                        $("#content").append("<tr>");
                        $("#content").append("<td class='store-row'><h5 property='seller'>"+this[MerchantName]+"</h5></td>");
                        $("#content").append("<td class='price-row'><h5 property='price'>$"+this[Price]+"</h5></td>");
                        $("#content").append("<td><a property='url' target='_blank' href='"+this[PageURL]+"' class='btn btn-danger'>GET IT</a></td>");
                        $("#content").append("</tr>");
                    })
                }
            });
        }
    </script>
    </head>
    <body>
        <a href="#" onClick="return false" onmousedown="javascript:swapContent('18');">18</a>
        <a href="#" onClick="return false" onmousedown="javascript:swapContent('48');">48</a>
        <a href="#" onClick="return false" onmousedown="javascript:swapContent('96');">96</a>

        <section class="price-sidebar span8" > 
        <div property="offers" typeof="Offer">               
            <h2>Price Comparison</h2>
            </br>
            <table class="price-data">
                <tr>
                    <th class='store-row'><h4>Store</h4></th>
                    <th class='price-row'><h4>Price</h4></th>
                </tr>
                <div id="content">

                </div>
            </table><!--end price-data-->
           </div><!--end offers-->
        </section><!--end price sidebar-->
    </body>

myphpscript.php

 <?php
    $countVar = $_POST['countVar'];

    $data = PriceCompare($countVar);
    echo json_encode($data);

    function PriceCompare($countVar){
        $DBH = new PDO('mysql:host=localhost;dbname=--','---','---');
        $STH = $DBH->query('SELECT ProductID, MerchantName, Price, PageURL
                            FROM merchants
                            WHERE ProductID=677 AND Count=' . $countVar . '
                            ORDER BY Price');
        $result = $STH->fetchAll();

        return $result;
       }
    ?>
4

1 回答 1

2

你有三个问题:

  1. 您的dataType: 'json'行末尾没有逗号。

  2. 访问您的数据,例如this.MerchantName. 如果this[MerchantName]您定义了一个名为MerchantName.

  3. 您将 a 附加<tr>#content,然后将 a 附加<td>#content,而不是将 附加<td><tr>.

试试这个:

$.each(data, function () {
    $("#content").append(
        $("<tr/>")
            .append("<td class='store-row'><h5 property='seller'>" + this.MerchantName + "</h5></td>");
            .append("<td class='price-row'><h5 property='price'>$" + this.Price + "</h5></td>");
            .append("<td><a property='url' target='_blank' href='" + this.PageURL + "' class='btn btn-danger'>GET IT</a></td>")
    );
})
于 2013-08-14T16:45:45.477 回答