我想讓用戶點擊使用AJAX的按鈕後,我的頁面顯示HTML textarea提交表單,但由於某種原因,AJAX的響應顯示除了textarea的。我對Web開發有點新,所以我的代碼很混亂。我從代碼中刪除了一些東西,以便於閱讀,但下面的代碼仍然無法正常工作。 JavaScript部分是:Javascript AJAX請求沒有返回HTML表單textarea
var time_variable;
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
var poll_id;
function getVote(choice, id, poll, display) {
switch(poll)
{
case 1:
poll_id = '1';
break;
case 2:
poll_id = '2';
break;
case 3:
poll_id = '3';
break;
default:
alert("Error in poll ID..");
}
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
//var txtname = document.getElementById("txtname");
xmlhttp.open("POST","poll_vote.php",true); //calling testing.php using POST method
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("choice=" + choice + "&id=" + id + "&disp=" + display); //Posting txtname to PHP File
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("poll"+poll_id).innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
點擊的按鈕是:
<div id="poll1">
<table id="pollm"><tr><td>
<a href="#" onclick="getVote(0,<?php echo $id; ?>,1,'m')" ><img src="/Images/A_not_pressed_small.png" border="0" onmouseover="this.src='/Images/A_pressed_small.png';" onmouseout="this.src='/Images/A_not_pressed_small.png';" /></a>
</td><td>
<?php echo $current['choice1']; ?>
</td></tr>
</table>
<table id="pollm"><tr><td>
<a href="#" onclick="getVote(1,<?php echo $id; ?>,1,'m')" ><img src="/Images/B_not_pressed_small.png" border="0" onmouseover="this.src='/Images/B_pressed_small.png';" onmouseout="this.src='/Images/B_not_pressed_small.png';" /></a>
</td><td>
<?php echo $current['choice2']; ?>
</td></tr>
</table>
</div>
而且poll_vote.php只是一個表:
<table width="200" border="0" cellpadding="0" cellspacing="0" >
<tr>
<form name="form1" method="post" action="add_comment.php">
<td><textarea name="a_answer" cols="45" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit" class="text_submit"></td>
<td>hello world!</td>
<td></td>
</tr>
</table></td></tr>
</table>
我點擊該按鈕後,我可以看到提交按鈕和「hello world!」 AJAX響應,但沒有textarea。任何人有任何建議嗎?謝謝
元素檢查員說什麼? – 2010-11-07 23:36:09
對不起,元素檢查器是什麼?是我的代碼的一部分? – Cody 2010-11-07 23:43:59
如果您使用的是Firefox,請安裝Firebug插件並調試您的JavaScript。 – 2010-11-07 23:45:06