Html 如果输入标签没有名称,表单数据是否仍会传输?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12543848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:58:26  来源:igfitidea点击:

Does form data still transfer if the input tag has no name?

htmlwebformsforms

提问by Jamie Kitson

For efficiency purposes I am wondering if a file or text in a textarea still gets transferred to the server if you omit the name attribute or set it to null. eg

为了提高效率,我想知道如果省略 name 属性或将其设置为 null,textarea 中的文件或文本是否仍会传输到服务器。例如

<input type="file" id="file" name="">
<textarea id="text" name="">

I notice that the data is not available at the server if you do this.

我注意到如果你这样做,数据在服务器上不可用。

回答by Petter

The W3C specification, if I understand it correctly, mandates that every form input element has a nameattribute specified. Otherwise that element will not be processed. Source

如果我理解正确的话,W3C 规范要求每个表单输入元素都name指定一个属性。否则将不会处理该元素。来源

回答by DenisS

No.

不。

I checked this in all browsers - the fields with empty/missing name are missing in POST/GET request from browser. It doesn't matter if they have or don't have id (my thought was that browsers might use id for name but no).

我在所有浏览器中检查了这一点 - 来自浏览器的 POST/GET 请求中缺少名称为空/缺失的字段。他们有没有 id 都没有关系(我的想法是浏览器可能使用 id 作为名称,但没有)。

回答by Dan Espinoza

it won't work directly but you can assign them through AJAX calls in JavaScript, idk really know if this really has any application in the real world ( one could be the obfuscation of the parameters that the server expects )

它不会直接工作,但您可以通过 JavaScript 中的 AJAX 调用分配它们,idk 真的知道这在现实世界中是否真的有任何应用程序(可能是服务器期望的参数混淆)

having

<form id="login" method="post" action="someurl">
 <input id="username" type="text" /> 
 <input id="password" type="password" />
 <input type="submit" value="login" />
</form>

JS to process would be (using jQuery to process ajax)

要处理的 JS 将是(使用 jQuery 处理 ajax)

$("#login").on("submit",function(ev){
  $.post("someurl",{
    usrn: $("#username").val,
    pwd: $("#password").val       
  },function(ev){
          //this is the callback function that runs when the call is completed successfully
  });
}
/*second argument on $.post is and object with data to send in a post request 
usrn would be the name of the parameter recived in the server 
same for pwd "#username" and  "#password" are the id's html attribute for the field
'.val' is the jquery object's attribute in which jquery access the value in the text box
"$()" or it's equivalent "jQuery()" works like an object constructor that fills 
the attributes with the
DOM data that cover the css selector that this function expects as a parameter*/

please NOTE code might not be fully correct since i haven't test it but the logic behind it should be self-explanatory

请注意代码可能不完全正确,因为我还没有测试过它,但它背后的逻辑应该是不言自明的