推送数组没有获得价值

时间:2015-05-25 11:01:08

标签: jquery arrays ajax

在下面的AJAX函数中,我正在尝试实现push数组。这个数组有问题:我没有在post中获取该数组值我在数组中获得该值但是在传递该值时它是未定义的。

  <script type="text/javascript">

  function getmember()
  {
    var myarray = new Array();
    myarray.push($(".group_id").val());
    alert(myarray);
     $.ajax({
      type: "GET",
      url: "getmember.php", 
      data: myarray,
      success: function(data) {
        $(".the-return").html(
          "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />JSON: " + data["json"]
        );

        alert("bb");
      }
    });
    return false;
  } 
  </script>

<select name="group_id" class="group_id" onChange="getmember();" multiple>

             <?php
               $selectGroup = "SELECT group_id,group_name FROM `group`";
               $groupRes           = mysql_query($selectGroup);
               while($row1 =  mysql_fetch_array($groupRes))
               {
                 echo '<td><option value="'.$row1['group_id'].'">'.$row1['group_name'].'</option></td>';
               }
             ?>
          </select>

1 个答案:

答案 0 :(得分:1)

使用key推送值并将变量myarray定义为全局

   var myarray; 
   function getmember()
   {
     myarray = [];
     myarray.push({'group_id':$(".group_id").val()});
    // your code.
   }

如所讨论的那样发布如下URL

的数据
http://localhost/ashutosh/getmember.php?group_id=1,2,3,4

更新代码:

 var myarray;
 function getmember()
 {
   myarray = [];
   myarray.push($(".group_id").val());
   // your code.

    url: "getmember.php", 
    data:'group_id='+myarray.join(),
 }