1

I have a search box that uses jQuery to submit the user's search and then display the results. However, for users that have JavaScript disabled this would not work so I have put the search box as a form so it will still work just by loading a different page instead.

However, now when I try and submit a search with JavaScript enabled it just submits the form and loads a new page as it would when JavaScript is disabled.

How can I resolve this? I hope you can understand my question.

I currently use the following jQuery code to submit my form:

$("#s").keyup(function(b){
if(b.keyCode==13){
if($(this).val().length>0){

And I have the following HTML:

<form action="/search" method="get">
<input type="text" id="s" maxlength="2048" name="q" autocomplete="off">
</form>

2 Answers 2

2
$('form').submit(function(Event) {
    Event.preventDefault();
    // Do your AJAX fancy submit stuff
    ...
});
Sign up to request clarification or add additional context in comments.

Comments

1

Why did you delete and re-add?

Return false should be enough. The following would run on this page will prevent the stackoverflow search from firing.

$("#search').submit(function(){ return false;});

In your case: $('#s').parent().submit(function(){ return false;});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.