2012-09-22 37 views

回答

47

如果我理解正確,W3C規範要求每個表單輸入元素都具有指定的name屬性。否則,該元素將不會被處理。 Source

+18

更確切地說,只有具有「name」屬性的字段(「控件」)對錶單數據集起作用。如果沒有這樣的屬性,該字段仍然可以正常處理,並且可以提交表單,而不需要該字段的數據。擁有沒有'name'屬性的字段不是錯誤。 –

+1

有人可以引用精確的HTML5嗎? –

24

我在所有的瀏覽器選中此 - 空/缺少的名稱中缺少POST領域/ GET從瀏覽器請求。無論他們是否擁有id(我的想法是瀏覽器可能使用id而不是id)。

0

它不會直接工作,但你可以通過AJAX的JavaScript調用它們分配,IDK的真正知道這是否真的有在現實世界中的任何應用程序(一個可能是服務器所期望的參數混淆)

具有

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

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*/ 

請NO TE代碼可能不完全正確,因爲我沒有測試它,但其背後的邏輯應該是不言自明的

相關問題