【问题标题】:Search with ajax for multiple fields使用ajax搜索多个字段
【发布时间】:2013-03-15 05:03:45
【问题描述】:

如果搜索字段是一个选择框,这里是一些示例代码

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

这两个与 ajax 相关的表单是单独工作的..

我想要的是这两个操作应该以单一形式和单一 ajax 脚本发生

谁能帮助我? 提前谢谢

【问题讨论】:

    标签: php mysql ajax search


    【解决方案1】:

    你可以使用类似 -

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

    【讨论】:

    • 你没有在函数()中初始化 str2
    猜你喜欢
    • 2012-10-29
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多