jQuery serializeArray不包含单击的提交按钮 [英] jQuery serializeArray doesn't include the submit button that was clicked
问题描述
我有一个带有两个按钮的表格.一个用于保存记录,另一个用于取消保存过程.我正在使用rails.js
(对于不认识的人来说,是一个常见的AJAX/jQuery插件) javascript文件,该文件可与jQuery配合使用,以进行友好的javascript/ajax调用.通过ajax发送表单数据时,我希望将单击的按钮的名称和值与其余数据一起提交,以便我可以根据单击的按钮来决定要做什么.
I have a form that has two buttons. One for saving a record and the other for cancelling the save procedure. I am using the rails.js
(a common AJAX/jQuery plug-in for those of you not in the know) javascript file that works with jQuery for unobtrusive javascript/ajax calls. When I send the form data over ajax, I want the name and value of the button I clicked to be submitted with the rest of the data so that I can make a decision on what to do based on which button was clicked.
rails.js
文件中的方法使用.serializeArray()
将表单数据发送到服务器.问题是这不包括我单击的按钮的名称/值对. jQuery的网站声明他们是故意这样做的(尽管我认为应该这样做):
The method in the rails.js
file uses .serializeArray()
for sending form data to the server. The problem is that this doesn't include the name/value pair of the button I've clicked. jQuery's website states that they do this on purpose (eventhough its my opinion that they should):
".serializeArray()
方法将标准W3C规则用于
"The .serializeArray()
method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute. No submit button value is serialized since the form was not submitted using a button."
他们怎么能假设没有使用按钮提交表单?我相信这是没有道理的,而且是错误的假设.
How can they assume that a form WASN'T submitted using a button? This makes no sense and a wrong assumption I believe.
根据W3C规则,为提交表单而激活的按钮被视为成功控件.
Under the W3C rules the button which was activated for the submission of a form is considered a successful control.
自从jQuery的开发人员决定故意这样做以来,我是否可以假定还有另一种方法不在序列化中排除激活的按钮?
Since the developers of jQuery have decided to do this on purpose, can I assume that there is another method that DOESN'T exclude the activated button in a serialization?
这是我的表单看起来像的简单示例...
<!DOCTYPE html5>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#form').submit(function(e) {
// put your breakpoint here to look at e
var x = 0;
});
});
</script>
</head>
<body>
<form id="form">
<input name="name" type="text"><br/>
<input name="commit" type="submit" value="Save"/>
<input name="commit" type="submit" value="Cancel"/>
</form>
</body>
推荐答案
没有,其行为基于 <form>
的submit
事件,而不是基于按钮,例如在JavaScript中按Enter或调用 .submit()
.您在这里混合了2个概念,一个 .serialize()
或
There is not, the behavior is based on the submit
event of the <form>
, not of a button, e.g. hitting enter or calling .submit()
in JavaScript. You're mixing 2 concepts here, a .serialize()
or .serializeArray()
may or may not have anything to do with a button click - it's just a separate event altogether, they're not connected. These methods at a higher level than that: you can serialize a form (or a subset of it) at any time for any reason.
但是,例如,如果您是通过按钮提交的,则可以像从该按钮提交的普通表单一样添加提交名称/值对:
You can however add the submit name/value pair like a normal form submitting from that button would, if you're submitting from a button for example:
$("#mySubmit").click(function() {
var formData = $(this).closest('form').serializeArray();
formData.push({ name: this.name, value: this.value });
//now use formData, it includes the submit button
});
这篇关于jQuery serializeArray不包含单击的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!