B Z*_*B Z 19 jquery jquery-plugins
我有一个表单有几个元素和两个提交按钮,一个是"保存",另一个是"删除".在"删除"按钮上,我使用jQuery对话框确认用户想要删除.如果他们确认,我提交表格.问题是jQuery.submit()在发布时不包括原始的提交按钮,因此我无法在服务器上区分删除和保存,因为它们都使用相同的表单.如果我删除jQuery对话框,则按预期发布提交按钮值.这个很常见,我希望有人可以分享解决方案.我已经四处寻找,找不到任何有用的东西(难道只是我或谷歌最近吸吮?)
谢谢你的帮助...
编辑:
提交按钮确实设置了名称和值.如果不使用jQuery.submit()它工作正常
Dav*_*och 18
根据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();
});
Run Code Online (Sandbox Code Playgroud)
更新:
我刚刚意识到上面的代码可能不是你想要的:
如果你在表单元素本身($("form").submit();
)上调用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);
});
});
Run Code Online (Sandbox Code Playgroud)
这将在提交表单之前将第一个按钮元素的名称和值添加到DOM(我不确定这里的默认浏览器行为是什么).请注意,这不会tempElement
从DOM中删除.如果出现错误并且表单未提交,则该元素将保留,如果您不处理此问题,则会遇到问题.