php - 下拉选择 onchange 更新多个 div

标签 php jquery html updates onselect

我有一个带有 onchange 事件的选择框,可以使用另一个文件中的内容更新 a 的内容。效果很好。 但我想做的是,当我从选择框中选择某些内容时,同时更新两个不同 div 中的内容。

所以我添加了另一个与第一个类似的函数,并将其添加到标签中的onchange中。

问题是它仍然只更新两者之一。 我究竟做错了什么 ? 或者是否有更简单的方法可以使用另一个页面的信息同时更新两个?

主页上的脚本:

<script>
function showFirstDiv(str) {
  if (str=="") {
    document.getElementById("div1").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("div1").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div1="+str,true);
  xmlhttp.send();
}

function showSecondDiv(str) {
  if (str=="") {
    document.getElementById("div2").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("div2").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div2="+str,true);
  xmlhttp.send();
}

主页上的html

<div id="users">
  <form>
    <select name="users" onchange="showFirstDiv(this.value); showSecondDiv(this.value);">
      <option value="">Select a person:</option>
      <option value="1">Peter Griffin</option>
      <option value="2">Lois Griffin</option>
      <option value="3">Joseph Swanson</option>
      <option value="4">Glenn Quagmire</option>
    </select>
  </form>
</div>

<!-- div1 -->
<div id="div1">
  <p>info to be listed in div 1 here</p>
</div>

<!-- div2 -->
<div id="div2">
  <p>info to be listed in div 2 here</p>
</div>

infopage.php

<?php

if ($_GET[info_div1] == '1')
    {
        echo 'info 1 to be showed in div 1';
    }
if ($_GET[info_div1] == '2')
    {
        echo 'info 2 to be showed in div 1';
    }
if ($_GET[info_div1] == '3')
    {
        echo 'info 3 to be showed in div 1';
    }
if ($_GET[info_div1] == '4')
    {
        echo 'info 4 to be showed in div 1';
    }

if ($_GET[info_div2] == '1')
    {
        echo 'info 1 to be showed in div 2';
    }
if ($_GET[info_div2] == '2')
    {
        echo 'info 2 to be showed in div 2';
    }
if ($_GET[info_div2] == '3')
    {
        echo 'info 3 to be showed in div 2';
    }
if ($_GET[info_div2] == '4')
    {
        echo 'info 4 to be showed in div 2';
    }

?>

最佳答案

两个函数都使用相同的全局变量xmlhttp。因此,当您调用第二个函数时,它会使用其 XMLHttpRequest 对象覆盖此变量。您应该使用局部变量,通过使用 var 声明它。

一般来说,您应该始终将变量声明为局部变量,除非您实际上需要将它们声明为全局变量。

function showFirstDiv(str) {
  var xmlhttp;
  if (str=="") {
    document.getElementById("div1").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("div1").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div1="+str,true);
  xmlhttp.send();
}

function showSecondDiv(str) {
  var xmlhttp;
  if (str=="") {
    document.getElementById("div2").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("div2").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div2="+str,true);
  xmlhttp.send();
}

关于php - 下拉选择 onchange 更新多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133239/

相关文章:

php - 如何加载没有php扩展名的php文件

php - 通过 ajax post 将 div Html 和 Css 保存在数据库中。

javascript - jQuery 设置 select 标签的值

javascript - 如何改变justGage中值的颜色

基于 PHP 的 MySQL 数据库管理器

php - 防止在特定时间之间注册

jquery - IE7大纲:0 not working

php - 如何从 jEditable 返回新值

html - 是否可以在 webapp 之外访问 html?

html - 将 CSS 类属性应用于 Firefox/IE 与 Chrome 中的链接元素