Javascript提交不包括提交按钮值 [英] Javascript Submit does not include Submit Button Value

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

问题描述

好吧,这不仅仅是我的信息问题(因为我可以想到4种不同的工作方式可以使它工作。但我有一个表格(没什么特别的)但是提交按钮有与之关联的特定值。

Ok, this is less of a question than it is just for my information (because I can think of about 4 different work arounds that will make it work. But I have a form (nothing too special) but the submit button has a specific value associated with it.

<input type='submit' name='submitDocUpdate' value='Save'/>

表单提交后我会检查该名称。

And when the form gets submitted I check for that name.

if(isset($_POST['submitDocUpdate'])){ //do stuff

但是,有一次我试图通过Javascript提交表单,而不是提交按钮。

However, there is one time when I'm trying to submit the form via Javascript, rather than the submit button.

document.getElementById("myForm").submit();

哪个工作正常,除了1个问题。当我查看通过javascript方法提交的$ _POST值时,它不包括submitDocUpdate。我得到表单的所有其他值,但不是提交BU tton值。

Which is working fine, except 1 problem. When I look at the $_POST values that are submitted via the javascript method, it is not including the submitDocUpdate. I get all the other values of the form, but not the submit button value.

就像我说的,我可以想到几种解决它的方法(使用隐藏变量,检查另一个表单变量上的isset等)但是我我只是想知道这是否是submit()的正确行为,因为它对我来说似乎不那么直观。在此先感谢。

Like I said, I can think of a few ways to work around it (using a hidden variable, check isset on another form variable, etc) but I'm just wondering if this is the correct behavior of submit() because it seems less-intuitive to me. Thanks in advance.

推荐答案

是的,这是 HTMLFormElement.submit()的正确行为

未发送提交按钮值的原因是因为HTML表单的设计是为了发送单击的提交按钮的值(或以其他方式激活)。这允许每个表单有多个提交按钮,例如您需要预览和保存操作的场景。

The reason your submit button value isn't sent is because HTML forms are designed so that they send the value of the submit button that was clicked (or otherwise activated). This allows for multiple submit buttons per form, such as a scenario where you'd want both "Preview" and a "Save" action.

因为您以编程方式提交了表单,单个提交按钮上没有明确的用户操作,因此不会发送任何内容。

Since you are programmatically submitting the form, there is no explicit user action on an individual submit button so nothing is sent.

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

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