我有这样的单子

John
   Jack (link)
       Husam (link)
   Koko (link)
       Rami (link)
       Loay (link)

我有一个下拉列表,上面有所有的名字,当我点击链接Husam侧时,我想在下拉列表中显示他的父(Jack)为selected选项,所以我需要传递给函数GET parent id链接点击的子的id,以及里面的这个id,这样我就可以把attr name传递给$GET['childId']而不是$GET['childId']=4。
我试图在php变量中保存链接的attr
是这样还是不可能?
这是我在index.php中的代码
$_GET['childId'] = 4; // here i don't want to pass 4 i need to pass $(a).attr('name'); from another page
if ($object->getParentId($_GET['childId'])) {
  echo "<script>
  function changeParent(){
    $(document).ready(function(){
      $('a').on('click',function() {
        var x = $(this).attr('id');
        var y = $(this).attr('name');
        $.ajax({
          type: 'POST',
          url: 'http://test.local/Family.php?action=getId',
          data: {'childId' : $_GET[childId]},
          success: function(msg) {
            document.getElementById('names').value = x;
            $('#save').show();
          }
        });
      });
    });
  }
  </script>";
}

这是我想传递给您的信息(a-ref的名称)
function getChild($family_tree,$parent){
$list = "<ul class='listSet' style='list-style-type:none'>";
foreach($family_tree[$parent] as $each_child) {
  $list .= "<li>" . $each_child[0]."  "."<a  onclick='changeParent()' id='$parent' name='$each_child[1]' href='#'>".'Change parent'."</a>";
  if(isset($family_tree[$each_child[1]])){...

最佳答案

好的,为了做到这一点,你必须对你的功能做一些改变。
这种情况下,您应该从if ($object->getParentId($_GET['childId']))移到index.php
下一步在Family.php里把这一行改成这一行

function changeParent(){
  $(document).ready(function(){
    $('a').on('click',function() {
      var x = $(this).attr('id');
      var y = $(this).attr('name');
      $.ajax({
        type: 'POST',
        url: 'http://test.local/Family.php?action=getId',
        data: {'childId' : y},
        success: function(msg) {
          document.getElementById('names').value = x;
          $('#save').show();
        }
      });
    });
  });
}

10-05 20:41
查看更多