簡體   English   中英

下拉選擇onchange以更新多個div

[英]dropdown select onchange to update multiple divs

我有一個帶有onchange事件的selectbox來更新來自另一個文件的內容。 它工作正常。 但我想要做的是當我從我的選擇框中選擇一些內容時,同時更新兩個不同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聲明它。

通常,除非實際需要將變量作為全局變量,否則應始終將變量聲明為local。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM