我有這個簡單的ajax代碼,出於某種原因,它不會將內容加載到textarea上。我需要它來加載內容,以便它可以在textarea中編輯。Ajax - 將內容加載到Textarea中
下面是代碼,我不知道我做錯了什麼。但它顯示在一個div像這樣
<div id="content"></div>
下面是完整的代碼
<html>
<head>
<title>Editing Page Content</title>
<script type="text/javascript">
function showCustomer(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("content").innerHTML="";
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("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","load.php?file="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p>Page Select to edit</p>
<?php
$result = mysql_query("SELECT pagename FROM site_content");
echo "<select name=\"pagename\" onchange=\"showCustomer(this.value)\" style=\"width:300px;\">";
echo "<option selected value=''>Please select a page to edit...</option>";
while($row = mysql_fetch_assoc($result)){
echo "<option selected value='" . $row['pagename'] ."'>" . $row['pagename'] . "</option>\n";
}
echo "</select>";
?>
<div id="content">
**it works here**
</div>
<textarea cols="50" id="area1" style="position: absolute; width: 700px; height: 300px;">
**It won't work here**
</textarea>
<input type="button" value="Submit content">
</div>
</body>
</html>
任何幫助,將不勝感激。
使用jQuery它的真棒! – binnyb 2012-01-12 22:36:13