当我加载页面时,它将显示家谱,并且每个孩子内部都有链接,并且在家谱下面有输入文本和下拉列表,其中包含每个父母和孩子,当我输入他的名字时添加新孩子选择他的父母。 我希望当我点击链接时,例如John内部的下拉列表中显示了他的父名。
这是我展示每件事的功能
public function GetFamilyTree(){
$query = "SELECT * FROM person";
$statment1 = $this->db->prepare($query);
$statment1->execute();
$family_tree = [];
$root_parent = -1;
$root_parent_name = "";
function getParent($statment1,&$family_tree,&$root_parent,&$root_parent_name){
while($row = $statment1->fetch(PDO::FETCH_ASSOC)){
if(is_null($row['parent'])){
$root_parent = $row['id'];
$root_parent_name = $row['nameOfPerson'];
}else{
if(!isset($family_tree[$row['parent']])){
$family_tree[$row['parent']] = [];
}
$family_tree[$row['parent']][] = array($row['nameOfPerson'],$row['id']);
}
}
}
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' id='change' href='#'>".'Change parent'."</a>";
if(isset($family_tree[$each_child[1]])){
$list .= getChild($family_tree,$each_child[1]);
}
$list .= "</li>";
}
$list .= "</ul>";
return $list;
}
getParent($statment1,$family_tree,$root_parent,$root_parent_name);
echo "<ul style='list-style-type:none'>";
echo "<li>$root_parent_name";
echo getChild($family_tree,$root_parent);
echo "</li>";
echo "</ul>";
}
这是我在index.php中的表单
<?php include_once('Family.php');
$object = new Family();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Family Tree</title>
</head>
<body>
<form method="post">
Child Name: <input type ='text' name ='name' placeholder="Enter name here">
<select name="parent" id="names" onchange="getSelectValue()">
<option>--Select Parent--</option>
<?php echo $object->displayParent() ?>
</select>
<br>
<input type="submit" name="submit" value="Enter">
</form>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
//Get selected nema
function getSelectValue(){
var selectedValue = document.getElementById("names").value;
console.log(selectedValue);
}
&#13;
我尝试了这个jquery,但它没有工作
$(document).ready(function() {
$( "#names" ).click(function() {
$("a").click(function() {
$("option#"+$(this).attr("id")).attr("selected", "selected");
});
});
});