[英]ajax, how to change two divs using one ajax request?
如何使用一個ajax請求更改2個div?
ajax-update.php
<?php
include("config.inc.php");
include("user.inc.php");
$id = $_GET['id'];
$item = New item($id);
$result = $item->iteminfo;
echo $result->name."<br />";
unset($item);
?>
的HTML:
<tr>
<td> (one) </td>
<td><input type="text" value="" onchange="showUser<?php echo $x;?>(this.value)" style="width:70px;"/></td>
<td><span id="txtHint<?php echo $x;?>"></span></td>
<td><input type="text" value="" name="price_<?php echo $x;?>" id="price_<?php echo $x;?>" onchange="upperCase<?php echo $x+$h;?>()" style="width:40px;"/></td>
<td><input type="text" id="proce_<?php echo $x;?>" style="width:70px;" value="<?php echo $x+$h;?>" disabled /></td>
<td><input type="text" value="" name="price_<?php echo $x;?>" style="width:40px;"/></td>
<td><input type='text' id="totalPrice<?php echo $x;?>" disabled /></td>
</tr>
腳本:
function showUser<?php echo $x;?>(str)
{
if (str=="")
{
document.getElementById("txtHint<?php echo $x;?>").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("txtHint<?php echo $x;?>").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax-update.php?id="+str,true);
xmlhttp.send();
}
我的問題是,當我更改此值時
<td><input type="text" value="" onchange="showUser<?php echo $x;?>(this.value)" style="width:70px;"/></td>
不僅這個td
<td><span id="txtHint<?php echo $x;?>"></span></td>
而且這個td
<td><input type="text" id="proce_<?php echo $x;?>" style="width:70px;" value="<?php echo $x+$h;?>" disabled /></td>
現在我只能即時更改一個div(txtHint),
為我的英語感到抱歉。
注意:我想用不同的文件來更新,例如當值改變時,這個腳本是word
xmlhttp.open("GET","ajax-update.php?id="+str,true);
另一個是
foo.php
謝謝
昂加
編輯1:
[1] [2] [3]
編輯2:
document.getElementById("txtHint<?php echo $x;?>").innerHTML=xmlhttp.responseText;
document.getElementById("proce_<?php echo $x;?>").value=xmlhttp.responseText;
多數民眾贊成在工作,但[2]和[3]中的值是相同的,因為它需要從一個文件ajax-update.php獲取如何添加另一個文件,但仍在一個ajax請求中?
如果我正確理解了您的問題,則希望更改第一個元素(SPAN)和第二個元素(INPUT)的文本。
您似乎已經確定了范圍-您使用innerHTML。 你寫了:
document.getElementById("txtHint<?php echo $x;?>").innerHTML=xmlhttp.responseText;
在此之下...您可以添加
document.getElementById("proce_<?php echo $x;?>").value=xmlhttp.responseText;
但是,您也應該考慮將jQuery用於此類事情。 它為您照顧了跨瀏覽器的工作,並使這種編碼更加簡單。 您可以執行所需的所有JSON / XML請求,並輕松更新單個組件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.