Javascript 如何通过PHP在AJAX调用中发送多个数据?

Javascript 如何通过PHP在AJAX调用中发送多个数据?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在尝试发送多个数据,在那里我点击了几个链接背靠背。事情是这样的 我有一个“共享”链接,如果我点击该链接,该链接将获得如下属性 $(".file").click(function(){ var elementFile = $(this); var id = elementFile.attr("name"); }); 接下来,单击此链接打开一个div(popDiv_共享),其中列出了数据库中的所有用户。现在,如果我单击任何一个用户,我将使用以下方法获得其属性,

我正在尝试发送多个数据,在那里我点击了几个链接背靠背。事情是这样的

我有一个“共享”链接,如果我点击该链接,该链接将获得如下属性

$(".file").click(function(){ 
          var elementFile = $(this);
      var id = elementFile.attr("name");
});
接下来,单击此链接打开一个div(popDiv_共享),其中列出了数据库中的所有用户。现在,如果我单击任何一个用户,我将使用以下方法获得其属性,如上所述:

$(".userlist").click(function(){ 
          var elementuser = $(this);
      var id = elementuser.attr("id");
});
现在我要坚持的是,我点击第一个链接,它会得到一个属性。现在,当我单击div中的第二个链接时,它也会得到属性,但是第一个链接属性就消失了。即使在背靠背单击每个链接之后,如何获取它们的两个属性,因为$(this)只会引用当前正在单击的链接,而上一个链接将丢失

任何建议都会大有裨益

这是我的密码:

<?php
    session_start();
    $user_id = $_SESSION['uid']; 
    include('db.php');

    $sql = "SELECT * from registered_users";
    $query = mysql_query($sql) or die(mysql_error());

    $result = mysql_query("SELECT * FROM user_files_public where uid = $user_id");

?>      

<!DOCTYPE html>
<html ng-app>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>

<script src = "http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="ajax-jaquery.js"></script>
<script type="text/javascript">

$(function(){
  $(".file").click(function(){
    $(".userlist").click(function(){
      var elementUser = $(this);
      var elementFile = $(".file").$(this);

      var name = elementUser.attr("id");
      var id = elementFile.attr("name");

      var info='name='+name+'&id='+id;

      $.ajax({
        type: "POST",
        url: "share.php",
        data: info,
        success: function(){
       }
     });
      $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");
return true;
    });
  });
});

</script>

<style>

        .ontop_share {
        z-index: 999;
        width: 900px;
        height: 900px;
        top: 0;
        left: 0;
        display: none;
        position: absolute;       
        background-color: #cccccc;
        color: #aaaaaa;
        opacity: 1.9;
        filter: alpha(opacity = 50);
      }
      #popup_share {
        width: 900px;
        height: 900px;
        position: absolute;
        color: #000000;
        border: 2px solid red;
        background-color: #ffffff;
        top: 50%;
        left: 50%;
        margin-top: -100px;
        margin-left: -150px;
      }

 </style>
<script type="text/javascript">
      function pop(div) {
        document.getElementById(div).style.display = 'block';
      }
      function hide(div) {
        document.getElementById(div).style.display = 'none';
      }

</script>

   </head>

    <body>

<div id="popDiv_share" class="ontop_share">

<div id="popup_share">
      <?php

  while($row = mysql_fetch_array($query)){
    $id = $row['id'];
    $name =$row['first_name'];?>

     <a href="" class="userlist" id="<?php echo $id;?>"><?php echo $id.' '.$name;?></a><?php  
    echo '<br/>';


  }
  ?>
  </div>  
            <a href="#" onClick="hide('popDiv_share')">Close</a>

</div>

    <table id="repos" align="center" cellspacing="20px">


            <thead>

                <tr>
                    <th>File name</th>
                    <th>Share</th>
                </tr>
            </thead>


                   <?php

                while($row = mysql_fetch_array($result)) { 
                    $id=$row['id'];
                    $user_id = $row['uid'];
                    $filename =$row['user_file_name'];


           ?> 

    <tbody>
        <tr>
            <td id ="actions"><?php echo $filename; ?></td>

            <td id ="actions">
                <a name="<?php echo $filename;?>" class="file" href="#" onClick="pop('popDiv_share')">Share</a>
            </td>


        </tr>
    </tbody>

    <?php     
          }                                
    ?> 
    </table>

</body>
</html>

$(函数(){
$(“.file”)。单击(函数(){
$(“.userlist”)。单击(函数(){
var elementUser=$(此值);
var elementFile=$(“.file”).$(此);
var name=elementUser.attr(“id”);
var id=elementFile.attr(“名称”);
var info='name='+name+'&id='+id;
$.ajax({
类型:“POST”,
url:“share.php”,
数据:信息,
成功:函数(){
}
});
$(this.parents(“.show”).animate({backgroundColor:”#003“},“slow”)
.animate({opacity:“hide”},“slow”);
返回true;
});
});
});
.ontop_share{
z指数:999;
宽度:900px;
高度:900px;
排名:0;
左:0;
显示:无;
位置:绝对位置;
背景色:#中交;
颜色:#AAAAA;
不透明度:1.9;
过滤器:α(不透明度=50);
}
#弹出式菜单共享{
宽度:900px;
高度:900px;
位置:绝对位置;
颜色:#000000;
边框:2倍纯红;
背景色:#ffffff;
最高:50%;
左:50%;
利润上限:-100px;
左边距:-150px;
}
函数pop(div){
document.getElementById(div.style.display='block';
}
函数隐藏(div){
document.getElementById(div.style.display='none';
}
文件名
分享
使用全局变量:

 var name;
    $(".file").click(function(){ 
              var elementFile = $(this);
          name= elementFile.attr("name");


    });
    $(".userlist").click(function(){ 
              var elementuser = $(this);
            var id = elementuser.attr("id");
          $.ajax({
         type: "POST",
         url: "share.php",
         data: {
            name: name,
            id: id 
         },
         success: function(){
          }
         });

    });
或遍历DOM:

$(".userlist").click(function(){ 
                var elementuser = $(this);
                var id = elementuser.attr("id");
                var name = $(this).closest('#popDiv_share').next('#repos').find('.file').attr("name");;
             $.ajax({
             type: "POST",
             url: "share.php",
             data: {
                name: name,
                id: id 
             },
             success: function(){
              }
             });

        });

您不需要使用
&
执行以下操作

 $.ajax({
        type: "POST",
        url: "share.php",
        data: {
            name: name,
            id: id 
           },
        success: function(){
       }
     });

希望有帮助:)

您可以将全局变量附加到
窗口,以便随时访问它们:

window.id;

$(".file").click(function(){ 
    var elementFile = $(this);
    window.id = elementFile.attr("name");
});
单击第二个链接后,您可以获取该
全局
变量并将其保存

$(".userlist").click(function(){
    var id = window.id;
    // rest of code 
}

我想这会管用的

事实上你误解了。如果向下滚动,则会出现一个“共享”链接,其中包含一个属性class=“file”。现在我单击这个链接,使用$(“.file”)获取它的属性;单击此按钮将打开一个具有用户列表的div。我单击任何用户,并以相同的方式获取属性。我想不出如何保留我先前单击的“共享”链接的属性,因为现在由于引用$(this)的脚本(这是当前的单击)而消失了。
$(".file").click(function(){
    var elementFile = $(this);
    $(".userlist").click(function(){
       var elementUser = $(this);
       var name = elementUser.attr("id");
       var id = elementFile.attr("name");
       var info='name='+name+'&id='+id;