在PHP变量中的Sava html attr

时间:2018-06-14 08:01:01

标签: javascript php jquery html dom

我有这样的列表

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

我的下拉列表中包含上面的所有名称,当我点击链接Husam方面我想在下拉列表中显示他的父母(杰克)作为选择的选项,所以我需要传递给函数getParentId的孩子的id链接点击,这个ID在里面,所以我如何将attr名称传递给$ _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>";
}

这是我想在Family.php(href的名称)

中传递的内容
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]])){...

1 个答案:

答案 0 :(得分:1)

好的,所以为了做到这一点,你必须对你的功能做一些改变。

此条件if ($object->getParentId($_GET['childId']))您应该从index.php移至Family.php

然后在$('a').on('click',function() {内,将此行data: {'childId' : $_GET[childId]},更改为此data: {'childId' : y},

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();
        }
      });
    });
  });
}