简体   繁体   English

如何使用锚标签提交带有jquery的表单

[英]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. 我想使用以下锚点向Spring提交带有jquery的表单。 How is this done? 这是怎么做到的?

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

I've tried this, where requestNew is my form: 我试过这个,其中requestNew是我的表格:

 $(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 将“YOUR_ACTION_PAGE_URL”替换为您的目标操作网址

HTML HTML

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

jQuery jQuery的

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

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

最简单的方法是:

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

if you add an id to your anchor you can submit the form in your click function: 如果您向锚点添加ID,则可以在单击函数中提交表单:

<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 如果你试图用ajax提交它是一个不同的解决方案,但我不确定这是否是你想要的根据你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM