最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Appending a hash '#' symbol to url with a form action? - Stack Overflow

programmeradmin4浏览0评论

So I've got a form with a submit button and a text input...

I want: http://localhost/

to become: http://localhost/#q=

I did a solution in javascript which involves changing the "action" of the form to the url with the hash onclick.. but this doesn't work in IE.

Anyone have a solution that works on all browsers?

So I've got a form with a submit button and a text input...

I want: http://localhost/

to become: http://localhost/#q=

I did a solution in javascript which involves changing the "action" of the form to the url with the hash onclick.. but this doesn't work in IE.

Anyone have a solution that works on all browsers?

Share Improve this question asked Jul 30, 2009 at 0:41 rawrrrrrrrrrawrrrrrrrr 3,6877 gold badges29 silver badges33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

I ran into a similar problem with IE not pulling in the hash from the form action.

I had a form

<form action="/#search" id="search-form">
   <input type="text" class="search-query" placeholder="Search" name="q">
</form>

When I submitted this form in anything but IE the page went to

/?q=searchparams#search

But in IE it went to

/?q=searchparams

To solve this I used JQuery to bind to the submit action and redirect to the page I wanted it to go to.

$("#search-form").submit(function() {
    var query = $('input[name="q"]').val();
    window.location.href = 'index.php?q='+query+'#search';
    return false;
}); 

It's been working fine since.

<script>
function add_hash() {
    window.location.hash = "q=";
}    
</script>

<form onsubmit="add_hash(); return false;">

Not sure what you're doing with this, though.

发布评论

评论列表(0)

  1. 暂无评论