jQuery serializeArray 不包括被点击的提交按钮 [英] jQuery serializeArray doesn't include the submit button that was clicked

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

问题描述

我有一个有两个按钮的表单.一个用于保存记录,另一个用于取消保存过程.我正在使用 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>

推荐答案

是否有另一种方法不会排除序列化中的激活按钮?

Is [there] another method that DOESN'T exclude the activated button in a serialization?

没有,行为基于

submit事件,而不是按钮的,例如点击输入或调用 .submit() 在JavaScript.您在这里混合了 2 个概念,一个 .serialize().serializeArray() 可能有也可能没有任何东西 与单击按钮有关 - 这完全是一个单独的事件,它们没有连接.这些方法处于更高的级别:您可以随时出于任何原因序列化表单(或其子集).

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 are 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屋!

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