我試過了w3schools的以下代碼。當我在輸入文本框中輸入字母時,應該相應地顯示建議。但是,沒有任何東西被顯示爲建議。我無法通過序列化獲取txt值。 在gethint.php中,由於它是一個post請求,我改變了原來的$ q = $ _ GET [「q」];到$ q = $ _ POST [「q」]。 有人能告訴我我做錯了什麼嗎?謝謝。ajax jquery php:w3school示例jquery ajax後不顯示暗示
post_example.html
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input").keyup(function(){
// txt=$("input").val();
txt=$("input").serialize();
$.post("gethint.php",txt,function(result){
$("span").html(result);
});
});
});
</script>
</head>
<body>
<p>Start typing a name in the input field below:</p>
First name:
<input type="text" >
<p>Suggestions: <span></span></p>
</body>
</html>
gethint.php
<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";
//get the q parameter from URL
$q=$_POST["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
試試這個'' – safarov 2012-03-27 05:55:06
你真的應該停下來使用w3schools網站。由於「爲什麼」的原因,請閱讀:http://w3fools.com/ – 2012-03-27 05:58:21