0

I'm trying to submit a form using jquery after modifying it's action attribute. The action attribute updates fine, but I was expecting the browser location to be the same as the action. But it's not that way. Do you see why?

Here's the form:

<form method="GET" class="modForm" action=""> <input type="text" placeholder="Search" class="modSearchValue"> <input type="radio" name="text" value="text" class="text" title="Search"> </form> 

Here's the jquery:

$('.modForm').submit(function(event) { var $this = $(this), var query = $this.find('.modSearchValue').val(); // Use val() instead of attr('value'). var locale = window.location; if($('.text').is(':checked')){query = '&text='+query;}else{query = '&handle='+query;} route = locale+query; console.log(route); if (query.length >= 1) { // Use URI encoding var newAction = (route); console.log(newAction); // DEBUG // Change action attribute $this.attr('action', newAction); //event.preventDefault(); } else { console.log('Invalid search terms'); // DEBUG // Do not submit the form event.preventDefault(); } }); 

Here, if the current location is http://localhost/search and the search term is 14, the action will be changed to http://localhost/search/?handle=14 and then submit. But for some reason, it wont.

1
  • 1
    var $this = $(this), should be var $this = $(this); Commented Jan 1, 2013 at 8:35

1 Answer 1

2

You have to use <form method="POST" class="modForm" action=""> method POST for it.......

remove comma var $this = $(this), and use var $this = $(this);

Sign up to request clarification or add additional context in comments.

2 Comments

remove comma var $this = $(this), and use var $this = $(this)
Very nice! Thanks, it works. In a funny way though. I'll post another question for that soon.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.