Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an anchor tag to submit a form with jquery

I want to use the following anchor to submit a form with jquery to Spring. How is this done?

<a target="" title="" class="" href="">Save</a>

I've tried this, where requestNew is my form:

 $(document).ready(function(){
  $("a").click(function(){
     $("#requestNew").submit(function(){
         $.post("../acctRequests", $("#requestNew").serialize());
      });
  });
 });

It doesn't seem to go anywhere.

like image 896
coder Avatar asked Jan 12 '11 22:01

coder


People also ask

Can we use anchor tag to submit form?

You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.

How can we submit a form using jQuery?

jQuery submit() Forms can be submitted either by clicking on the submit button or by pressing the enter button on the keyboard when that certain form elements have focus. When the submit event occurs, the submit() method attaches a function with it to run. It triggers the submit event for selected elements.

How do you use tag in form?

The <form> tag is used to create an HTML form for user input. The <form> element can contain one or more of the following form elements: <input> <textarea>

How do I submit a form to a click of link?

In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.


2 Answers

You are adding a new event handler; all you need to do is trigger the existing ones, and the browser's native functionality:

$(document).ready(function(){
  $("a").click(function(){
     $("#requestNew").submit();
  });
});
like image 166
lonesomeday Avatar answered Sep 30 '22 19:09

lonesomeday


Please use this

<a href="javascript:document.YOUR_FORM_NAME.submit()"> Submit</a>

<form name="YOUR_FORM_NAME" action="YOUR_ACTION_PAGE_URL" method=POST>
           <input type="text" name="YOUR_INPUT_NAME" value="YOUR_INPUT_VALUE"/>
      </form>

Replace "YOUR_ACTION_PAGE_URL" with your targeted action url

like image 34
fatih.abdullah Avatar answered Sep 30 '22 19:09

fatih.abdullah