admin管理员组文章数量:1389762
I am writing a client-side rendering web application using react
and react-router
for routing. I have implemented a search with an input
inside a form
with react-bootstrap
.
executeSearch(){
...
window.location.href = '/#/search/' + formattedURL
}
let formSettings = {
onSubmit:this.executeSearch.bind(this),
id:"xnavbar-form"
}
const searchButton =
<Button
onClick={this.executeSearch.bind(this)}>
<Glyphicon glyph="search" />
</Button>
const searchInput =
<form>
<Input
type="text"
ref="searchInput"
buttonAfter={searchButton}
placeholder="search"/>
</form>
... Jumping to the render function
<Navbar.Form
{...formSettings}>
{searchInput}
</Navbar.Form>
Executing a search in Mozilla Firefox
works as expected: Hitting the enter key and clicking on the searchButton
route me to a client-side rendered page .../#/search/...
.
Executing the search in Google Chrome
behave like this: Clicking the searchButton
works like above (client side render), but hitting the enter key cause a server redirect .../?#/search...
.
I figured out it is caused by the form's submit
attribute, how can I modify it to render on the client?
I was thinking about listening to a keyboard ascii code (enter) for executing the search, but it seems a bit messy.
I am writing a client-side rendering web application using react
and react-router
for routing. I have implemented a search with an input
inside a form
with react-bootstrap
.
executeSearch(){
...
window.location.href = '/#/search/' + formattedURL
}
let formSettings = {
onSubmit:this.executeSearch.bind(this),
id:"xnavbar-form"
}
const searchButton =
<Button
onClick={this.executeSearch.bind(this)}>
<Glyphicon glyph="search" />
</Button>
const searchInput =
<form>
<Input
type="text"
ref="searchInput"
buttonAfter={searchButton}
placeholder="search"/>
</form>
... Jumping to the render function
<Navbar.Form
{...formSettings}>
{searchInput}
</Navbar.Form>
Executing a search in Mozilla Firefox
works as expected: Hitting the enter key and clicking on the searchButton
route me to a client-side rendered page .../#/search/...
.
Executing the search in Google Chrome
behave like this: Clicking the searchButton
works like above (client side render), but hitting the enter key cause a server redirect .../?#/search...
.
I figured out it is caused by the form's submit
attribute, how can I modify it to render on the client?
I was thinking about listening to a keyboard ascii code (enter) for executing the search, but it seems a bit messy.
Share Improve this question asked Jan 30, 2016 at 17:59 itaieditaied 7,10713 gold badges59 silver badges94 bronze badges 5-
If you are only switching the hash (and not the page location and hash) try just changing location.hash instead the
href
– Patrick Evans Commented Jan 30, 2016 at 18:07 - api.jquery./jquery.ajax – Daan Commented Jan 30, 2016 at 18:10
-
I do need to change the route, since I'm using
react-router
as my "ponents loader", it's not just changing the hash. And what is it to do withajax
? I don't want to send a request to the server... – itaied Commented Jan 30, 2016 at 18:13 - Have you ever heard about AJAX or Google? And more duplicated answers bellow... – felipsmartins Commented Jan 30, 2016 at 18:35
- Have you ever heard about client side rendering? AJAX has nothing to do with it. – itaied Commented Jan 30, 2016 at 18:39
1 Answer
Reset to default 5Stop the default behaviour of a form submit by using event.preventDefault()
.
This will prevent the browser from submitting your form and instead leave the "routing" logic to you to do what you want it to.
Just accept the event in your executeSearch
function.
executeSearch(event){
...
event.preventDefault();
window.location.href = '/#/search/' + formattedURL;
}
本文标签: javascriptHow to submit a HTML form without redirectingStack Overflow
版权声明:本文标题:javascript - How to submit a HTML form without redirecting? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744741425a2622629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论