admin管理员组

文章数量:1296391

I have this search bar:

<div id="tfheader">
    <form id="tfnewsearch" method="get" action="">
            <input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
    </form>
<div class="tfclear"></div>
</div>

It worked great but when I search "keyword", it will redirect to /?q=keyword.

How to redirect to ? Thank you very much !

I have this search bar:

<div id="tfheader">
    <form id="tfnewsearch" method="get" action="http://mywebsite.">
            <input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
    </form>
<div class="tfclear"></div>
</div>

It worked great but when I search "keyword", it will redirect to http://mywebsite./?q=keyword.

How to redirect to http://mywebsite./keyword ? Thank you very much !

Share edited Dec 20, 2014 at 13:55 baao 73.3k18 gold badges150 silver badges207 bronze badges asked Dec 20, 2014 at 12:49 BanBan 1311 gold badge2 silver badges9 bronze badges 2
  • possible duplicate of .htaccess rewrite query string as path – Terry Commented Dec 20, 2014 at 12:53
  • You need to hook into form submit event: stackoverflow./questions/27409950/… After you have done that, you also need to make sure your server can understand this request. – dfsq Commented Dec 20, 2014 at 13:02
Add a ment  | 

1 Answer 1

Reset to default 5

You can do it with javascript

<script>
var a = document.getElementById('tfnewsearch');
a.addEventListener('submit',function(e) {
e.preventDefault();
var b = document.getElementById('tftextinput').value;
window.location.href = 'http://mywebsite./'+b;

});

</script>

and give your input field an ID called tftextinput.

<input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120">

I don't really understand why you would like to do this, as it is way more difficult to handle this request server side as it would be to simply handle the $_GET data you will receive when doing a standard form transmission.

EDIT:

Here is the full code:

<div id="tfheader">
    <form id="tfnewsearch" method="get" action="http://www.mywebsite.">
        <input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
    </form>
<div class="tfclear"></div>
</div>

<script>
    var a = document.getElementById('tfnewsearch');
    a.addEventListener('submit',function(e) {
        e.preventDefault();
        var b = document.getElementById('tftextinput').value;
        window.location.href = 'http://mywebsite./'+b;

    });

</script>

本文标签: javascriptHTML How to create search barStack Overflow