Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajaxphp+;MySQL-选择框_Javascript_Php_Mysql_Ajax_Drop Down Menu - Fatal编程技术网

Javascript ajaxphp+;MySQL-选择框

Javascript ajaxphp+;MySQL-选择框,javascript,php,mysql,ajax,drop-down-menu,Javascript,Php,Mysql,Ajax,Drop Down Menu,我有一个简单的AJAX示例,我想修改它,所以当我在选择框中更改年份时,我希望它对多个div生效,而不仅仅是ID为的div。有解决方案吗?我正在考虑getelementsbyclassname,但它没有显示任何内容 <html> <head> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; ret

我有一个简单的AJAX示例,我想修改它,所以当我在选择框中更改年份时,我希望它对多个div生效,而不仅仅是ID为的div。有解决方案吗?我正在考虑getelementsbyclassname,但它没有显示任何内容

<html>
<head>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").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").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","proba.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>    
    <select name="users" onChange="showUser(this.value)">
      <option value="">Choose year:</option>
      <option value="2013">2013</option>
      <option value="2014">2014</option>    
    </select>
</form>
<br>

<div id="txtHint"><b> - </b></div>

<p>&nbsp;</p>
</body>
</html>

函数showUser(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“proba.php?q=“+str,true”);
xmlhttp.send();
}
选择年份:
2013
2014

-


GetElementsByCassName
可以正常工作,您只需迭代返回的项目:

<html>
<head>
<script>
function showUser(str) {
  if (str=="") {
    setContent("");
    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) {

      setContent(xmlhttp.responseText);

    }
  }
  xmlhttp.open("GET","proba.php?q="+str,true);
  xmlhttp.send();
}

function setContent(content){
    var divs = document.getElementsByClassName('txtHint');  //fixed syntax error here
    var i = divs.length;

    while(i--) {
          divs[i].innerHTML = content;
    }
}
</script>
</head>
<body>

<form>    
    <select name="users" onChange="showUser(this.value)">
      <option value="">Choose year:</option>
      <option value="2013">2013</option>
      <option value="2014">2014</option>    
    </select>
</form>
<br>

<div class="txtHint"><b> - </b></div>
<div class="txtHint"><b> - </b></div>
<div class="txtHint"><b> - </b></div>

<p>&nbsp;</p>
</body>
</html>

函数showUser(str){
如果(str==“”){
设置内容(“”);
返回;
} 
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
setContent(xmlhttp.responseText);
}
}
open(“GET”、“proba.php?q=“+str,true”);
xmlhttp.send();
}
函数setContent(content){
var divs=document.getElementsByClassName('txtHint');//修复了此处的语法错误
var i=分段长度;
而(我--){
divs[i].innerHTML=content;
}
}
选择年份:
2013
2014

- - -


什么都没有是什么意思?尝试添加console.log(xmlhttp);在onreadystatechange函数的顶部,用结果更新答案。我的意思是,复制+粘贴您的解决方案,它不会显示任何内容。@VaimanHunor OK有一个小语法错误,我使用了逗号而不是分号。这个问题现在已经解决了