index.php
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str,str1) {
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 (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getdata.php?q="+str+"&s="+str1,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="0">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>
<select name="users2" onchange="showUser(this.value)">
<option value="0">Select a person:</option>
<option value="5">Peter Griffin</option>
<option value="6">Lois Griffin</option>
<option value="7">Joseph Swanson</option>
<option value="8">Glenn Quagmire</option>
</select>
</form>
<br>
<div id="txtHint">
<b>
Person info will be listed here.
</b>
</div>
</body>
</html>
从这个HTML页面,我想将两个值传递给一个ajax函数,但是它不能从第二个下拉列表传递值。但是从名称为“ users”的第一个下拉列表中,可以将值很好地传递给ajax,但是从第二个下拉列表中,它不能将值传递给ajax函数。
在下面检查getdata.php getdata.php
<?php
echo "ab";
echo $_GET['s'];
if($_GET['q']==1)
{
echo "7";
}
if($_GET['q']==2)
{
echo "2";
}
if($_GET['q']==3)
{
echo "3";
}
if($_GET['s']==7)
{
echo "4";
}
?>
原因是因为您一直在寻找第一个选择框值而不是选择框。将函数更改为此:
function showUser() {
var str=document.getElementById("user").value;
var str1=document.getElementById("user2").value;
if (str==0) {// if nothing is selected from first drop down
document.getElementById("txtHint").innerHTML="";
return;
}
.......
........//rest of the code
}
和您对此的选择:
<select name="users" id="user" onchange="showUser()"><!--added id here in select-->
<select name="users2" id="user2" onchange="showUser()"><!--added id here in select-->
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句