当其他元素检索元素时,jQuery无法正常工作

时间:2013-12-01 15:09:12

标签: javascript jquery json

这是主页:命名为'index.php'

<html>
<head>
<script src='jquery.min.js' ></script>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="No match found";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();

}

function adduser()
{
    var fname=document.getElementsByName("fname")[0].value;
    var lname=document.getElementsByName("lname")[0].value;
    var age=document.getElementsByName("age")[0].value;
    var ht=document.getElementsByName("ht")[0].value;
    var job=document.getElementsByName("job")[0].value;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
        xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
            }
        xmlhttp.open("GET","adduser.php
q="+fname+"&w="+lname+"&e="+age+"&r="+ht+"&t="+job,true);
    xmlhttp.send();
}

$(document).ready(function(){

    $("#hidelist").click(function(){
        $("#list").hide("fast");
        return false;
    });

});

</script>
</head>
<body>

<button type='button' onclick='showUser(this.value)' id='getdata' value='1' >get data</button>

<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

这是单击id ='getdata'的按钮时的页面。命名为'getuser.php'

<?php

error_reporting(0);
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','rathena','ajax_demo');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user";

$result = mysqli_query($con,$sql);

echo "<div id='list' ><table border='1' style='margin-top: 0px;' >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table></div>";

echo "<input type='text' name='fname' value='Firstname' /><br />";
echo "<input type='text' name='lname' value='Lastname' /><br />";
echo "<input type='text' name='age' value='Age' /><br />";
echo "<input type='text' name='ht' value='Hometown' /><br />";
echo "<input type='text' name='job' value='Job' /><br />";
echo "<button style='display: non;'  type='button' onclick='adduser()' >add user     data</button>";

echo "<button style='display: non;' type='button' id='hidelist' >hide list</button>";

mysqli_close($con);
?>

但是,我想要实现的是,每当我点击id ='hidelist'的按钮时,用id ='list'隐藏div。但是jQuery代码没有抓住我检索到的页面的按钮ID。在这一行:

$(document).ready(function(){

    $("#hidelist").click(function(){
        $("#list").hide("fast");
        return false;
    });

});

我很欣赏有关此问题的任何想法。谢谢:))

1 个答案:

答案 0 :(得分:4)

您必须将事件委托给最近的非动态父元素,因为附加事件处理程序时#hidelist元素不存在:

$(document).ready(function(){
    $('#txtHint').on('click', '#hidelist', function(){
        $("#list").hide("fast");
    });
});