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);
    – voigtan
    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);

2
  • remove comma var $this = $(this), and use var $this = $(this) Jan 1, 2013 at 8:39
  • Very nice! Thanks, it works. In a funny way though. I'll post another question for that soon.
    – Norman
    Jan 1, 2013 at 8:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.