0

このチュートリアルを使用してAJAXでインスタント検索を構築しました。インスタント結果が得られますが、問題は、結果が表示されるdivが開いたままになることです。

すぐに結果が出た後のように修正したいのですが

  1. ページのどこかをクリックすると、result(div)が消えます。
  2. 入力フィールドにもう一度マウスオーバーすると、result(div)が再表示されます。

AJAXコード

    <script type="text/javascript">
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("search-result").innerHTML="";
  document.getElementById("search-result").style.border="0px";
  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("search-result").innerHTML=xmlhttp.responseText;
    document.getElementById("search-result").style.border="1px solid #A5ACB2";
    }
  }
xmlhttp.open("GET","instant-search.php?keyword="+str,true);
xmlhttp.send();
}
</script>

HTMLコード

    <div id="search">
<form action="" method="get" id="search-form" >

<input name="" type="" size="" id="search-input" onkeydown="showResult(this.value)"/>

<select name="" id="search-select">
<option value="all" >All</option>
<input type="hidden" name="" value="" />
</select>

<input type="submit" value="Search" id="search-btn" /> 

</form>      
</div>
<div id="search-result"></div>

PHPコードは、単純なMySQL全文検索クエリです。

これを追加してみましたが何も起こりません

        <input name="keyword" type="text" size="50" id="search-input" onkeydown="showResult(this.value)" onclick="(this.value==this.defaultValue) this.value='';"  onmouseout="showResult(this.value='';)"/>

これを行う方法を提案してください。

ありがとう。

4

2 に答える 2

0

厄介な方法

<body>
<div id="main" onclick="fx(0)">

<div id="search">
<form action="" method="get" id="search-form" >
<input name="" type="" size="" id="search-input" onkeydown="showResult(this.value)"/>
<select name="" id="search-select">
<option value="all" >All</option>
<input type="hidden" name="" value="" />
</select>
<input type="submit" value="Search" id="search-btn" /> 
</form>      
</div>
<div id="search-result" onclick="fx(1)"></div>

</div>
</body>

<script>
function fx(var flag)
{
  // document.getElementById(theIdYouWantToShowHide).style.display="none" or inline or block ...
}
</script>
于 2012-08-02T13:34:32.827 に答える
0

最後にそれを手に入れました。これが他の人にも役立つことを願っています。

 <script type="text/javascript">
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("search-result").innerHTML="";
  document.getElementById("search-result").style.border="0px";
  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("search-result").innerHTML=xmlhttp.responseText;
    document.getElementById("search-result").style.border="1px solid #A5ACB2";
    document.getElementById("search-result").style.display="block";
    document.getElementById("search-input").onmouseover = function(){show_box()};
    }
  }
xmlhttp.open("GET","instant-search.php?keyword="+str,true);
xmlhttp.send();
}

function close_box(){
document.getElementById("search-result").style.display="none";
}
 function show_box(){
    document.getElementById("search-result").style.display="block";
}

document.onclick = function(){close_box()};

</script>
于 2012-08-02T14:42:21.423 に答える