dousu5608 2014-05-22 15:42
浏览 56
已采纳

Chrome浏览器调试器将jQuery语句作为“未定义函数”返回

I have code below that does work. It makes the .ajax call to my php file, retrieves the array of data as required.

However, when I start looping through the array, I am attempting to dynamically create DOM elements within an existing table. At the point of these function calls, I get the error "undefined functions", note that that commented out alerts DO work, so I know that data is being returned, and the loop IS being entered. I just cannot figure out why my syntax to create the dom elements and attributes is not working.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
                    <script type="text/javascript"> 
                        $.ajaxSetup({
                            cache: false
                        });
                         $(document).ready(
                        function(){
                            $.ajax({
                                url: "PHPLibrary/selectMemberResults.php",
                                type: "POST",
                                dataType: "json",
                                success: function (data) { 
                                    //alert("1 Total number of rows of data >" + data.length + "<"); 
                                        for (var j = 0; j < data[i].length; j++) {
                                            //alert("2 Total number of rows of data >" + data[i].length + "<");
                                            $memName = data[i][0];
                                            $busName = data[i][1];  
                                            //alert($memName);  
                                            //alert($busName); 
                                            (function ($) {
                                            $('tr').attr({width:"100%",id:"row"}).appendto('#memberResults');  
                                            $('td').text($memName).appendto("row");
                                            $('tr').attr({width:"100%",id:"row"}).appendto('#memberResults');  
                                            $('td').text($busName).appendto("row");
                                            }(jQuery)); 
                                }
                            });
                        }); 
                    </script>

One final note, I've gone through and checked that all curly braces and brackets have matching pairs, in the correct order, as do all of the parenthesis which are terminated with a semicolon (;).

Any thoughts or help would be greatly appreciated.

Thanks!!!!

  • 写回答

1条回答 默认 最新

  • doushang8846 2014-05-22 15:45
    关注

    If you are trying to create elements your syntax is incorrect; currently you are selecting elements. For example this code:

    $('tr').attr({ width: "100%", id: "row" }).appendTo('#memberResults');
    

    Is selecting all tr elements in the page and appending them to #memberResults. Instead you need to use this syntax:

    $('<tr />').attr({ width: "100%", id: "row"}).appendTo('#memberResults');
    

    Note the HTML-like angle brackets in the selector. You can also use the second parameter of the jQuery object to apply the needed attributes to the new element, so your final code becomes:

    $('<tr />', { width: "100%", id: "row" }).appendTo('#memberResults');
    

    Finally, it should be appendTo() not appendto(), and also be careful you don't duplicate id attributes with your appended elements. I would suggest using a class on them instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?