单击按钮时,我的输入标签变空

时间:2018-03-30 22:03:47

标签: javascript html

当我点击按钮时,我创建了一个添加一些输入的表单。这个按钮的功能是用javascript完成的。​​

但是当我点击它时,我的所有输入都变空了,我不希望这样。我希望将信息保存在我已有的输入中。

出了什么问题,我该如何解决?

? Please create a new profile or overwrite the existing profile.
 [Nikhil Sanjay Wagh]       "default"
(node:3316) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '1' of null
    at inquirer.prompt.then (C:\Users\Nikhil Wagh\AppData\Roaming\npm\node_modules\ask-cli\lib\init\init.js:87:55)
    at <anonymous>
(node:3316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3316) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
var div = document.getElementById('participant');

function AjoutParticipant() {
  var ex = document.getElementsByClassName("bouton_plus");
  for (i = 0; i < ex.length; i++) {
    ex[i].style.display = "none";
  }
  div.innerHTML += '<label>Nom :</label><input type="text" name="nom[]" />' +
    '<label id="pre-decal">Prenom :</label><input type="text" name="prenom[]" />' +
    '<label id="ent-decal">Entreprise :</label><input type="text" name="entreprise" /><br/>' +
    '<button class="bouton_plus" type="button" onclick="AjoutParticipant()"><i class="fa fa-fw fa-plus"></i> Ajouter Participant</button>'
}

1 个答案:

答案 0 :(得分:1)

您正在使用div的innerHTML,为其添加一些新HTML,然后将其分配回来。

HTML中的value 属性表示默认值,因此当用户在其中键入内容时,它不会更新。

因此,当您覆盖innerHTML时,将输入重置为默认值。

请勿使用innerHTML

相反,使用createElementappendChild等DOM方法向div添加新内容,而不重写现有内容。

相关问题