GetJSON函数不起作用

时间:2013-10-13 16:29:51

标签: jquery json

我把一切都搞定了,但我的表格显示未定义。我需要我的.getJSON函数工作。我不打电话给我什么?或者我需要在我的.getJSON函数中添加或删除什么?我的JSON文件中的数组是:

[
    {name: enteredBy, text: entryText}  
]

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Blog Demo</title>
        <style>
            .control {
                margin-bottom:10px;
            }

            .control label {
                display:block;
                margin-bottom:3px;
            }

            table {
                border:1px solid #000;
                background-color:#FFFED1;
                width:500px;
            }

            table caption {
                border-top:1px solid #000;
                border-left:1px solid #000;
                border-right:1px solid #000;
                font-size:1.5em;
                background-color:#466180;       
                color:#fff;
                line-height:1.5em;
                padding:5px;                
            }

            table thead {
                background-color:#AAECC2;
            }

            table tbody {
                margin-top:15px;                
            }

            table tbody td {
                background-color:#fff;
            }           

        </style>
    </head>
    <body>      
        <div>
            <div class="control">
                <label for="enteredBy">Entered By</label>
                <input type="text" id="enteredBy"/>
            </div>
            <div class="control">
                <label for="blogEntry">Blog Entry</label>
                <textarea id="blogEntry" cols="50" rows="5"></textarea>
            </div>
            <div class="control">
                <button id="postEntry">Post</button>        
            </div>
        </div>
        <table>
            <caption>
                Blog Entries
            </caption>
            <thead>
                <tr>
                    <th>Entered By</th>
                    <th>Entry</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <script src="scripts/jquery-1.10.2.min.js"></script>
        <script>
            $(function() {
                var blogEntries = [];


                $("#postEntry").click(function() {

                    var enteredBy = $("#enteredBy").val();
                    var entryText = $("#blogEntry").val();



                    $.getJSON(
                    'entries.js',
                    function(allBlogEntries) {
                        blogEntries=getBlogEntriesForName(blogEntriesForName, allBlogEnries);
                        displayBlogEntries(blogEntries);

                    }

                    );


                blogEntries.push(blogEntry);    

                    // Now display the blog entries
                    displayBlogEntries();                   
                });

                function displayBlogEntries() {
                    var tableRows = "";

                    //TODO: Loop through the blogEntries array. For each item in the array create an HTML table row.


                        $.each(blogEntries, function(key, value) {
                        var tableRow = "<tr>";
                        tableRow += "<td>" + value.name + "</td>";
                        tableRow += "<td>" + value.text + "</td>";
                        tableRow += "</tr>";

                        tableRows += tableRow;
                    });
                    $("table tbody").html(tableRows);
                }
            });


        </script>
    </body>
</html>

0 个答案:

没有答案