jQuery submit()不包括提交的按钮

栏目: jQuery · 发布时间: 7年前

内容简介:我有一个表单有几个元素和两个提交按钮,一个是“保存”,另一个是“删除”.在“删除”按钮上,我使用jQuery对话框确认用户想要删除.那么如果他们确认,我提交表格.问题是jQuery.submit()在发布时不包含原始的提交按钮,所以我无法在服务器上区分“从保存中删除”,因为它们都使用相同的形式.如果我删除了jQuery对话框,那么提交按钮的值按预期的方式发布.这是很常见的,我希望有人可以分享一个解决方案.我已经搜索,找不到任何有用的东西(只是我或者是最近吮吸谷歌?)感谢任何帮助…编辑:

我有一个表单有几个元素和两个提交按钮,一个是“保存”,另一个是“删除”.在“删除”按钮上,我使用jQuery对话框确认用户想要删除.那么如果他们确认,我提交表格.问题是jQuery.submit()在发布时不包含原始的提交按钮,所以我无法在服务器上区分“从保存中删除”,因为它们都使用相同的形式.如果我删除了jQuery对话框,那么提交按钮的值按预期的方式发布.这是很常见的,我希望有人可以分享一个解决方案.我已经搜索,找不到任何有用的东西(只是我或者是最近吮吸谷歌?)

感谢任何帮助…

编辑:

提交按钮确实设置了名称和值.它工作正常,如果不使用jQuery.submit()

基于karim79的答案:

$("input[type=submit], input[type=button], button").click(function(e) {
    var self= $(this),
        form = self.closest(form),
        tempElement = $("<input type='hidden'/>");

    // clone the important parts of the button used to submit the form.
    tempElement
        .attr("name", this.name)
        .val(self.val())
        .appendTo(form);

    // boom shakalaka!
    form.submit();

    // we don't want our temp element cluttering up the DOM, do we?
    tempElement.remove();

    // prevent default since we are already submitting the button's form.
    e.preventDefault();
});

更新:

我只是意识到上面的代码可能不是你要找的:

如果你正在调用form表单元素($(“form”).submit();),你将需要这样的东西:

$("form").submit(function(){
    var form = $(this);
    $("input[type=submit], input[type=button], button", form).eq(0).each(function(){
        var self= $(this),
            tempElement = $("<input type='hidden'/>");

        // clone the important parts of the button used to submit the form.
        tempElement
            .attr("name", this.name)
            .val(self.val())
            .appendTo(form);
    });
});

这将在表单提交之前将第一个按钮元素的名称和值添加到DOM(我不知道默认浏览器的行为在这里).请注意,这不会从DOM中删除tempElement.如果出现错误,并且表单未提交,则该元素将保留,如果您不理会该问题,您将遇到问题.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/4605671/jquery-submit-doesnt-include-submitted-button


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Haralambos Marmanis、Dmitry Babenko / Manning Publications / 2009-7-8 / GBP 28.99

Web 2.0 applications provide a rich user experience, but the parts you can't see are just as important-and impressive. They use powerful techniques to process information intelligently and offer featu......一起来看看 《Algorithms of the Intelligent Web》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换