我有一个表单有几个元素和两个提交按钮,一个是“保存”,另一个是“删除”.在“删除”按钮上,我使用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],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.如果出现错误,并且表单未提交,则该元素将保留,如果您不理会该问题,您将遇到问题.