Javascript 使用ajax显示php响应文本

Javascript 使用ajax显示php响应文本,javascript,php,ajax,select,Javascript,Php,Ajax,Select,我有一个函数onChange-my-select,它显示来自php的一些文本。我使用ajax来完成这个请求,而不需要重新加载整个页面。 我的问题是,一个类有很多div,我想显示ajax与我更改的select匹配的结果: <div class="x"> <select class="mySelect"> <option>1</option> <option>2</option&

我有一个函数onChange-my-select,它显示来自php的一些文本。我使用ajax来完成这个请求,而不需要重新加载整个页面。 我的问题是,一个类有很多div,我想显示ajax与我更改的select匹配的结果:

    <div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere">php text requested with ajax</div>
    </div>

<div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere">php text requested with ajax</div>
    </div>

<div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere">php text requested with ajax</div>
    </div>

我想在这里显示的div类中显示响应,但在右边的div中,换句话说,在与我的select匹配的div中,我已经更改了,明白吗?我不知道这是否可能

你肯定需要一些东西来区分div。可以给它们一个ID,并将其作为事件中的参数传递给AJAX函数

div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere" id="first">php text requested with ajax</div>
    </div>

<div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere" id="second">php text requested with ajax</div>
    </div>

<div class="x">
      <select class="mySelect">
        <option>1</option>  
        <option>2</option>
      </select>   
      <div class="showHere" id="third">php text requested with ajax</div>
    </div>


In your script:

function ajaxCall(id_of_target_div) {
    ...
    document.getElementById(id_of_target_div).innerHTML = xmlhttp.responseText ;
}

或者,您可以使用事件的发送者来缩短整个事件。在下面的元素中填充AJAX调用的结果,这要求div总是直接位于select之后

通过这段代码,您可以获得触发事件的元素。如果用户使用ie无法正确解释event.target,则event.src元素是一个后备元素:

var target = event.target || event.srcElement;
此部分在选择后选择下一个图元:

var nextDiv = target.nextSibling;
现在添加您的答复:

nextDiv.innerHTML = xmlhttp.responseText;

你描述了你自己的问题,你所有的元素都是一样的。如果需要专门针对元素,则需要为它们指定一种方法。html ID可以很好地实现这一点。
nextDiv.innerHTML = xmlhttp.responseText;