2013-03-15 78 views
0

下面是一個如果搜索領域的一些示例代碼,這是一個選擇框搜索與阿賈克斯多個領域

<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> 

</body> 
</html> 

這是輸入字段的HTML代碼

<html> 
<head> 
<script> 
function showHint(str) 
{ 
if (str.length==0) 
    { 
    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","gethint.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 

<p><b>Start typing a name in the input field below:</b></p> 
<form> 
First name: <input type="text" onkeyup="showHint(this.value)"> 
</form> 
<p>Suggestions: <span id="txtHint"></span></p> 

</body> 
</html> 

這兩阿賈克斯相關表單正在單獨工作..

我想要的是那兩個操作應該發生在單一形式和單個阿賈克斯腳本

任何人都可以幫助我嗎? 由於事先

回答

0

您可以使用類似 -

<html> 
<head> 
<script> 
function showHint(str,type) 
{ 
    var str2 = ''; 
    if (type == 1) 
    { 
     if (str.length==0 && document.getElementById('users').value == '') 
     { 
      document.getElementById("txtHint").innerHTML=""; 
      return;   
     } else { 
      str2 = document.getElementById('users').value; 
     } 
    } else { 
     if (str.length==0 && document.getElementById('text').value == '') 
     { 
      document.getElementById("txtHint").innerHTML=""; 
      return;   
     } else { 
      str2 = document.getElementById('text').value; 
     } 
    } 

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","gethint.php?q="+str+'&str2 = '+str2,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 

<p><b>Start typing a name in the input field below:</b></p> 
<form> 
First name: <input type="text" id = "text" onkeyup="showHint(this.value,'1');"> 
<select name="users" id = 'users' onchange="showHint(this.value','2');"> 
<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> 
<p>Suggestions: <span id="txtHint"></span></p> 

</body> 
</html> 
+0

ühavnt INITIALISE STR2函數() – 2013-03-15 05:50:49

+0

你需要在服務器端兩個值,對不對? – 2013-03-15 06:06:16

+0

是的。你可以建議 – 2013-03-15 06:36:37