double2022 2018-06-11 07:26
浏览 62
已采纳

点击孩子来得到他的父母

I have a family tree and dropdown also has all name of parent and child, inside each child there is link, i want when i clicked on any child link displayed his father in dropdown (make it option selected). Please someone help me.

this is function to display the child

function getChild($family_tree,$parent){
        $list = "<ul style='list-style-type:none'>";
        foreach($family_tree[$parent] as $each_child){
            $list .= "<li>" . $each_child[0]."  "."<a style='text-decoration:none ; font-size: smaller' onclick='changeParent()' href='#'>".'Change parent'."</a>";
            if(isset($family_tree[$each_child[1]])){
                $list .= getChild($family_tree,$each_child[1]);
            }
            $list .= "</li>";
        }
        $list .= "</ul>";
        return $list;

        }

and this function to get the father of child

 public function getParentId($childId)
 {
    $statment = $this->db->prepare("SELECT parent FROM `person` WHERE id = $childId");
    $statment->execute();
    $result = $statment->fetchAll();

    foreach($result as $output){

        return $output['parent'];

    }
 }

and this is my form

<form method="post">
  Child Name: <input type ='text' name ='name' placeholder="Enter name here">
  <select name="parent" id="names" onchange="getSelectValueId()">
  <option>--Select Parent--</option>
  <option><?php echo $object->displayParent()?></option>
  </select>
    <br>
    <input type="submit" name="submit" value="Enter">
</form>

Update **** Now i get the child id when i clicked on link and i has function that get parent id ... how i can change the option selected now ?? any one can help me plz !!!!

this is the function gets the id of child from link

 function changeParent(){
      $(document).ready(function(){
        $("a").click(function() {
           function getEventTarget(e) {
        e = e || window.event;
        return e.target || e.srcElement;
        }

        var ul = document.getElementsByClassName('listSet');
        [].forEach.call(ul, function(el) {
        el.onclick = function(event) {
            var target = getEventTarget(event);
           alert(target.id);
        };
        });
      });
     });
    }
            

</div>
  • 写回答

1条回答 默认 最新

  • donglilian0061 2018-06-11 11:02
    关注

    I solved it :))) this is the answer But i solved it with out call function that get parent id :/

    function changeParent(){
          $(document).ready(function(){
            $("a").click(function() {
               function getEventTarget(e) {
            e = e || window.event;
            return e.target || e.srcElement;
            }
    
            var ul = document.getElementsByClassName('listSet');
            [].forEach.call(ul, function(el) {
            el.onclick = function(event) {
                var target = getEventTarget(event);
               //alert(target.id);
               document.getElementById('names').value=target.id; //This line to change selected option to parent of child clicked.
            };
            });
      }); 
     });      
          }

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大