全局变量行不起作用

时间:2016-05-13 10:54:57

标签: jquery

这是我的jQuery代码:

$(document).ready(function() {
    var row = [];
    $('tr').click(function() {
        $tds = $(this).find("td");             // Finds all children <td> elements
        $.each($tds, function(index) {         // Visits every single <td> element
            row[index] = $(this).text();
        });
        console.log(row[6]); // this has values
    });
    console.log(row[6]); //this returns "undefined"
});

1 个答案:

答案 0 :(得分:0)

$(document).ready(function(){

     var row = [];

    $('tr').click(function(){
         $tds = $(this).find("td");             // Finds all children <td> elements

          $.each($tds, function(index) {               // Visits every single <td> element

                  row[index] = $(this).text();

          });// end loop


    /*

        $.fn.returnRow = function(){
                console.log("row inside the anonymous:" +row);
                return row;
         }
           console.log


          */
          console.log("what does row "+row[6]);
        $('#myModalLabel'+row[6]).text(row[0]);


      });


    $('#save'+row[6]).click(function(){
         console.log(row[6]);

                 alert("You have clicked Send sms to:"+row[0]);
                 var message = $("#content"+row[6]).val();


                      $.post("sendSms.php", 
                        {
                          phoneNumber: row[0],
                          linkId: row[2],
                          message: message

                        },function(data,status){
                           $('#save'+row[6]).html('Sent');
                          console.log(data);
                          console.log(status);


                      });

    });

});