tags:

views:

41

answers:

5

hi,

I need to call a function on a using jquery. This function needs to submit a form. Is this possible?

The reason i need something like this is that i need to have the submission of form happen at two links ie the submission should happen if the user clicks on any of the 2 links. The values of the form are set in as hidden fields so there is no user input except the click on the href.

+1  A: 

try this

$(document).ready(function(){
  $("#selector").click(function(event){ 
    $("form").submit();
    return false;
  }
});
JapanPro
+1  A: 
$('a.link').click(function(){
   $('form').submit();
   return false;
});
Reigel
+1  A: 

Best practice based on what you're trying to achieve: say you have two elements that should submit the form with the id's element_1 and element_2:

$(function(){
    $("#element_1, #element_2").click(function(){
        $("form").submit();
    });
});

If the elements are a link you do not want to be followed, you can append return false; to the end of the call, but if they're simply input buttons, that shouldn't be necessary.

yuval
`return false();` should be just `return false;`. Unless you have a `function false(){ return false; }` declaration. ^_^
Reigel
good catch! edited, thanks :)
yuval
A: 

You can use .ajax() function:

jQuery('#yourlinkid').live('click', function(event) {
    $.ajax({
        url     : submitform.php,
        type    : POST,
        data    : $('#yourformid').serialize(),
        success : function( data ) {
                        alert(data);        
                   }
    });
});
NAVEED
A: 

It's better practice to use the event method preventDefault, instead of returning false.

$('a.link').click(function (event) {
    $('form').submit();
    event.preventDefault();
});
xmarcos