1

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.

5 Answers 5

1

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.

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

1 Comment

return false(); should be just return false;. Unless you have a function false(){ return false; } declaration. ^_^
1
$('a.link').click(function(){
   $('form').submit();
   return false;
});

Comments

1

try this

$(document).ready(function(){
  $("#selector").click(function(event){ 
    $("form").submit();
    return false;
  }
});

Comments

0

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);        
                   }
    });
});

Comments

0

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

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

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.