如何从提交按钮获取formmethod和formaction [英] How to get formmethod and formaction from submit button

查看:139
本文介绍了如何从提交按钮获取formmethod和formaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题:有没有一种方法可以达到构造效果?或找出表单中的哪个按钮被点击,从按钮访问它们?



我是否需要重写我的事件处理程序以捕捉按钮上的点击( $(document).on('click','.myformbtn',function(e))并使用 var queryString = $(this).parent( 'form')。serialize(); 来访问表单。

 < form> 
< button type =submit
formmethod =POST
formaction =/ mysave>保存< /按钮>

<按钮类型= submit
formmethod =GET
formaction =/ mydata>获取新数据< / button>
< / form>

$(文档).ready(function()
{
$(document).on('submit','form',function(e){
e.preventDefault(); $ b $ (); $ var $ for $ {$ this()).getFormAction();
var queryString = $(this).serialize();
$ b $ .ajax({
type:formmethod,
url:formaction,
data:queryString
});
});
});

我发现了类似的问题,答案总是 $(this)。 attr('formaction')这是不正确的,因为表单没有该属性。我希望提供一个如何使用它的例子,以便让人们的大脑工作。

解决方案

考虑到你的代码,我们可以得到按以下方式导致表单提交的按钮:

  var target = e.originalEvent || e.originalTarget; 
var clickedElement = $(target.currentTarget.activeElement);

$(document).ready(function(){$(document))>

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script>< ;形式>这是带有2个提交按钮的表单< br>< br> < button type =submitformmethod =POSTformaction =/ mysave>保存< / button> < button type =submitformmethod =GETformaction =/ mydata>获取新数据< / button>< / form>  


I would have thought the form object would have been populated with formmethod and formaction.

Question: Is there a way to get to formaction? Or find out which button in the form was clicked, to access them from the button?

Do I need to rewrite my event handler to catch clicks on the button ($(document).on('click', '.myformbtn', function(e)) and use var queryString= $(this).parent('form').serialize(); to access the form.

<form>
    <button type="submit"
            formmethod="POST"
            formaction="/mysave">Save</button>

    <button type="submit"
            formmethod="GET"
            formaction="/mydata">Get new data</button>
</form>

$(document).ready(function()
{
    $(document).on('submit', 'form', function(e) {
        e.preventDefault();
        var formaction = $(this).getFormAction();
        var queryString= $(this).serialize();

        $.ajax({
                type: formmethod,
                 url: formaction,
                data: queryString
        });
    });
});

I have found similar questions and the answer is always $(this).attr('formaction') which is incorrect as the form does not have that attribute. I'm hoping providing an example of how it would be used will get peoples brains working.

解决方案

Considering your code we can get the button which caused the form submission in following way:

var target = e.originalEvent || e.originalTarget;
var clickedElement = $( target.currentTarget.activeElement);

$(document).ready(function()
{
    $(document).on('submit', 'form', function(e) {
        e.preventDefault();
        
	var target = e.originalEvent || e.originalTarget;
        var clickedElement = $( target.currentTarget.activeElement);
        
	var formaction = $(clickedElement).attr("formaction");
        var formmethod = $(clickedElement).attr("formmethod");
      
	    alert(" formaction "+formaction); 
        
		var queryString= $(this).serialize();

        $.ajax({
                type: formmethod,
                 url: formaction,
                data: queryString
        });
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
  This is my form with 2 submit buttons<br><br>
    <button type="submit"
            formmethod="POST"
            formaction="/mysave">Save</button>

    <button type="submit"
            formmethod="GET"
            formaction="/mydata">Get new data</button>
</form>

这篇关于如何从提交按钮获取formmethod和formaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆