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

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.

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

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?