微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

确定从jQuery / JavaScript 中点击了哪个提交按钮

参见英文答案 > How can I get the button that caused the submit from the form submit event?12个
我从一个没有写的程序中打开一些表单.为此,我想出了这个聪明的解决方案:
jQuery("form").submit(function(event) {
    // get some values from elements on the page:
    var the_form = jQuery(this);
    var data = the_form.serialize();
    var url = the_form.attr( 'action' );
    var button = event.originalEvent.explicitOriginalTarget;

    data = data + "&" + button.name + "=" + button.value;

    // Send the data using post and put the results in a div
    jQuery.post( url,data,function() {
        //Do something crazy
    });

    // stop form from submitting normally
    if (event.preventDefault) 
    { 
        event.preventDefault(); 
    } 
    else 
    {
        event.returnValue = false; 
    }
});

哪个工作完美.我走开欣喜.问题是,我无意中使用Mozilla / Gecko属性来确定点击了哪个按钮. (event.originalEvent.explicitOriginalTarget)这意味着这只适用于Firefox.

原文地址:https://www.jb51.cc/jquery/180108.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐