Javascript InnerHTML Erases Data In Form Elements

前端 未结 4 1880
灰色年华
灰色年华 2021-01-22 05:32

I have this form with a button that allows you to add fields to the form.

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 05:47

    When you set the innerHTML property, the browser parses the HTML and creates a new DOM tree for it, then replaces the current DOM tree with the new one. This destroys any data that isn't in the HTML.

    You need to append DOM elements using createElement and appendChild or using jQuery (easier).

提交回复
热议问题