How to POST HTML form using `id` instead of `name`

前端 未结 3 1483
粉色の甜心
粉色の甜心 2021-01-22 19:27

This following code will get element by name, I\'m gonna change it to get element by ID, how to make it?



        
3条回答
  •  庸人自扰
    2021-01-22 19:48

    By default, an HTML form submits values keyed on the name attribute of the element.

    You can modify this behavior using JavaScript by writing a function that handles the onsubmit action of the form and performing the submit yourself.

    1. When the user hits or otherwise submits the form, the submit event is triggered
    2. If you have a JavaScript function set to handle onsubmit, it will be called at this point
    3. Your function should then create an HTTP POST request to the desired server resource (i.e. the one specified in the action attribute of the form and containing the code in the question). The POST data sent with this request can be keyed on ids or any other identifier you wish to use.
    4. Your function should then cancel the form submit that would otherwise be initiated by the browser (e.g. return false)

提交回复
热议问题