2012-02-21 3 views
1

私はcodeigniterに取り組んでおり、コントローラの検索クラスにマップしたい検索バーがあります。 URLは現在search/searchtermです。私はこれに準拠したフォームを構築し、フォームにあるものをすべて取り出し、URLに渡すだけです。フォーム付きのカスタムURL

document.location.href= "<?= base_url() ?>" + document.forms["searchbox"]["search"].value 

が、動作していないよう:私はちょうどこのようONSUBMIT javascriptの再配置を行ってみました。あなたはこのようなもののために最高のものとして何をお勧めしますか?何人もの人が自分のプロジェクトで欲しいものに似た何かを必要としていると確信しています。

答えて

3

は、別のサンプルです。これは、有効なxhtmlドキュメントであり、検索ボックスに入力したものをグーグルで検索します。 FF10、IE8、Chrome 17、Opera 11.61で動作します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Search</title> 
    <script type="text/javascript"> 
     window.onload = function(){ 
      var form = document.getElementById("form1"); 
      form.onsubmit = function(){ 
       var searchText = document.getElementById("searchText"); 
       window.location = "http://www.google.com/search?q=" + searchText.value; 
       return false; 
      };   
     }; 
    </script> 
</head> 
<body> 
    <form id="form1" method="post" action=""> 
     <div> 
      <label for="searchText">Search:</label> 
      <input type="text" id="searchText" name="searchText" /> 
      <input type="submit" value="Click" /> 
     </div> 
    </form> 
</body> 
</html> 
0

これは、フォームの内部にない場合は、フォームの中でそれを行うためには、適切なアクションをフォームに設定するためにonsubmitイベントを設定する必要があります。ここで

は簡単な作業例である:ここでは

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <script type="text/javascript"> 
     function foo() { 
      document.forms["searchbox"].action= window.location.href+"/" + document.forms["searchbox"]["search"].value; 
      return true; 
     } 
    </script> 
</head> 
<body> 
    <form id="searchbox" onsubmit="foo()" > 
     <input type="text" id="search" /> 
     <input type="submit" /> 
    </form> 
</body> 
</html> 
関連する問題