tags:

views:

75

answers:

3

Hi, I'm trying to have the action of a HTML form built when the user clicks the submit button.

So the user fills in a form, clicks submit, then the action gets built then it actually gets submitted. The reason being is because the form has a load of options on it which will be passed to a script.

How would I go about doing this with Jquery?

+3  A: 

Untested, but this should at least get you on your way:

$('#myForm').submit(function (event)
{
    var action = '';
    // compute action here...
    $(this).attr('action', action);
});
Matt Ball
Perfect, thanks!
Probocop
+2  A: 

Use jQuery submit event:

$(document).ready(function() {
  $('#yourFormId').submit(function() {
    $(this).attr('action', 'dynamicallyBuildAction'); 
    return false;
  });
});
Jarek
Perfect, Thanks!
Probocop
A: 

The plain Javascript solution would have a function:

function changeAction() {
  this.action = 'the dynamic action';
  return true;
}

In the form you would set the onsubmit event:

<form ... onsubmit="return changeAction();">

To do the same using jQuery would be:

$(function(){
  $('IdOfTheForm').submit(function(){
    this.action = 'the dynamic action';
    return true;
  });
});
Guffa