开发者

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="" titl开发者_Python百科e="" 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.


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


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


Assign the submit handler outside the click. Then call it from the click.

$(document).ready(function(){
       // Binds the submit handler to the #requestNew form
   $("#requestNew").submit(function(){
        $.post("../acctRequests", $("#requestNew").serialize());
   });
   $("a").click(function(e) {
        $("#requestNew").submit(); // calls the submit handler
        e.preventDefault();  // Prevents the default behavior of the link
   });
});


HTML

<form...>
    ...
    <a data-role="submit">Save</a>
</form>

jQuery

$(function(){
    $("[data-role=submit]").click(function(){
        $(this).closest("form").submit();
    });
});


if you add an id to your anchor you can submit the form in your click function:

<a target="" title="" class="" href="" id="saveButton">Save</a>
$(document).ready(function(){
  $('#saveButton').click(function(){
    $("#requestNew").submit(); //if requestNew is the id of your form
  });
});

If you are trying to submit it with ajax that's a different solution but I'm not sure if that's what you want based on your question


the simplest way is this:

<a href="#" onclick="submit()">Submit</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜