简体   繁体   English

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

[英]ajax, how to change two divs using one ajax request?

how to change 2 divs using one ajax request ? 如何使用一个ajax请求更改2个div?

ajax-update.php 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: 的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>

script: 脚本:

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();
}

my question is, when i change this value 我的问题是,当我更改此值时

<td><input type="text" value="" onchange="showUser<?php echo $x;?>(this.value)"  style="width:70px;"/></td>

not only this td 不仅这个td

<td><span id="txtHint<?php echo $x;?>"></span></td>

but also this td 而且这个td

    <td><input type="text"  id="proce_<?php echo $x;?>" style="width:70px;" value="<?php echo $x+$h;?>" disabled /></td>

now i only can change one div (txtHint) on the fly, 现在我只能即时更改一个div(txtHint),

sory for my english. 为我的英语感到抱歉。

note : i want to put un update with diferent file, for example when value change, this script is word 注意:我想用不同的文件来更新,例如当值改变时,这个脚本是word

xmlhttp.open("GET","ajax-update.php?id="+str,true);

and the other one is 另一个是

foo.php

thanks 谢谢

angga 昂加

EDIT 1: 编辑1:

[1] | [1] [2] | [2] [3] [3]

  • [1] : i input value manualy [1]:我手动输入值
  • [2] : this span txtHint change using ajax [2]:此跨度txtHint使用Ajax更改
  • [3] : i want this change too same as [2] [3]:我希望此更改与[2]相同

EDIT 2 : 编辑2:

document.getElementById("txtHint<?php echo $x;?>").innerHTML=xmlhttp.responseText;
document.getElementById("proce_<?php echo $x;?>").value=xmlhttp.responseText;

thats is work, but value in [2] and [3] is same, because it take from one file ajax-update.php how to add other file but still in one ajax request ? 多数民众赞成在工作,但[2]和[3]中的值是相同的,因为它需要从一个文件ajax-update.php获取如何添加另一个文件,但仍在一个ajax请求中?

If I understand your question correctly, you want to change the text for the first element (a SPAN) as well as the second (an INPUT). 如果我正确理解了您的问题,则希望更改第一个元素(SPAN)和第二个元素(INPUT)的文本。

The span you seem to have figured out -- you use innerHTML. 您似乎已经确定了范围-您使用innerHTML。 You wrote: 你写了:

document.getElementById("txtHint<?php echo $x;?>").innerHTML=xmlhttp.responseText;

Underneath that... you could add 在此之下...您可以添加

document.getElementById("proce_<?php echo $x;?>").value=xmlhttp.responseText;

However -- you should also look into using jQuery for this kind of thing. 但是,您也应该考虑将jQuery用于此类事情。 It takes care of the cross-browser stuff for you, and makes this kind of coding MUCH simpler. 它为您照顾了跨浏览器的工作,并使这种编码更加简单。 You can do all the JSON/XML requests you want, and update individual components easily. 您可以执行所需的所有JSON / XML请求,并轻松更新单个组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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