javascript - Ajax応答を使用してテーブルを書き換える方法
javascriptとajaxを使用してテーブルを書き直そうとしています。ここで要求が送信され、応答が届きましたが、innerHTMLは応答テキストでテーブルコンテンツを再書き込みできません。
これが私のコードです。
<table>
<tbody id='product1'>
<tr>
<td> Product Name:</td>
<td>P1</td>
</tr>
</tbody>
</table>
Javascript
function compareProd(prodID,tId){
browse();
var url = 'process.jsp';
url += '?compareProd='+prodID;
xmlHttp.onreadystatechange = changeProd(tId);
xmlHttp.open('POST', url, true);
xmlHttp.send(null);
}
function changeProd(tID) {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
document.getElementById('product1').innerHTML = xmlHttp.responseText;
}
}
process.jsp
<tr>
<td> Product Name:</td>
<td>P2</td>
</tr>
ここに応答が到着しましたが、テーブルを書き換えることはできません。
単一の関数で試して、
xmlHttp
リクエストオブジェクトを初期化したと想定して、次のように確認してください。
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById('product1').innerHTML = xmlHttp.responseText;
}
};