繁体   English   中英

将值从jquery变量传递到php变量

[英]Pass value from jquery variable to php variable

我需要通过单击表行来获取ID,并在另一个php文件中使用该ID。 为此,我通过jquery获取ID,代码为

var id = $(this).closest("tr").find('td:eq(0)').text(); //工作正常

而且我想将此ID作为php变量传递。 但是任何人都行不通。 我的代码是:

$(document).ready(function(){
  $("#people-table tr").click(function(){
    var id = $(this).closest("tr").find('td:eq(0)').text();
     $.ajax({
        url: "default_people.php",
        type: "GET",

        data: { id1: id},
        success: function (result) {
                alert(id);
        }
    });
  });
});

和我的PHP代码是:

  $a = $_GET['id1'];
  echo $a ;

您的选择器已经是tr元素:

var id = $(this).find('td:eq(0)').text(); // use this

因此您不必通过.closest()遍历tr *,可以将其删除。

在成功函数中,您使用了错误的参数:

success: function (result) {
     alert(result); // <-------update to this
}

php端,您可以使用isset()方法:

if(isset($_GET['id'])){
  $a = $_GET['id1'];
  echo $a ;
}

*实际取决于您的dom结构。

试试jquery $.get

$(document).ready(function(){
  $("#people-table tr").click(function(){
    var id = $(this).closest("tr").find('td:eq(0)').text();
     $.get("default_people.php?id="+id);
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM