繁体   English   中英

ajax,如何使用一个ajax请求更改两个div?

[英]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]

  • [1]:我手动输入值
  • [2]:此跨度txtHint使用Ajax更改
  • [3]:我希望此更改与[2]相同

编辑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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM