我需要在更改两个不同的选择器时填充两个不同的表,每个表应指向相应的表。我尝试过不同的东西,但我认为我看错了路。任何的想法?任何提示?
<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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint2"><b>Person info will be listed here.</b></div>
</body>
</html>
答案 0 :(得分:0)
您可以在函数中添加第二个参数:
function showUser(str, hintId) { /* ... */}
添加传递当前提示div的元素id,如下所示:
<select name="users" onchange="showUser(this.value, 'txtHint')">
...
<select name="users" onchange="showUser(this.value, 'txtHint2')">
然后,当您使用getElementById
时,代码中的任何位置都会使用hintId
参数。
document.getElementById(hintId).innerHTML="";