jQuery Submit()不包含提交按钮 [英] jQuery submit() doesn't include submitted button

查看:87
本文介绍了jQuery Submit()不包含提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个元素和两个提交按钮的表单,一个是保存",另一个是删除".在删除"按钮上,我正在使用jQuery对话框确认用户要删除.然后,如果他们确认,我将提交表格.问题是jQuery.submit()在发布时不包含原始的提交按钮,因此我无法在服务器上区分从保存中删除",因为它们都使用相同的表单.如果我删除了jQuery对话框,则提交按钮值将按预期发布.这很常见,我希望有人可以分享解决方案.我到处搜索,找不到有用的东西(仅仅是我还是Google最近吸吮?)

I have one form that has several elements and two submit buttons, one is "Save" the other is "Delete". On the "Delete" button I am using jQuery dialog to confirm user wants to delete. Then if they confirm, I submit the form. The problem is jQuery.submit() doesn't include the original submit button when it is posted so I can't distinguish on the server a Delete from a Save since they are both using the same form. If I remove the jQuery dialog then the submit button value is posted as expected. This HAS to be very common and I am hoping someone can share a solution. I've searched around and can't find anything useful (is it just me or is google sucking lately?)

感谢您的帮助...

提交按钮确实设置了名称和值.如果不使用jQuery.submit(),效果很好

The submit buttons do have names and values set. It works fine if don't use jQuery.submit()

推荐答案

基于karim79的答案:

Based on karim79's answer:

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


更新:


UPDATE:

我刚刚意识到上面的代码可能不是您想要的:

I just realized the above code is probably not what you are looking for:

如果要在表单元素本身($("form").submit();)上调用Submit,则将需要以下内容:

If you are calling submit on the form element itself ($("form").submit();) you will need something like this:

$("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中添加 first 按钮元素的名称和值(我不确定这里的默认浏览器行为是什么).请注意,这不会从DOM中删除tempElement.如果发生错误并且未提交表单,则该元素将保留,并且如果您不注意这一点,将会遇到问题.

Which will add the first button element's name and value to the DOM right before the form is submitted (I'm not sure what the default browser behavior is here). Note that this doesn't remove the tempElement from the DOM. If there is an error and the form isn't submitted the element will remain and you will have problems if you don't take care of this.

这篇关于jQuery Submit()不包含提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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