【问题标题】:Retrieve input values from dinamically genrated HTMLElement form?从动态生成的 HTML 元素表单中检索输入值?
【发布时间】:2020-05-15 21:20:21
【问题描述】:

我目前在一个 Angular 项目中(我是初学者),我创建了一个函数来根据 JSON 的内容创建一个 HTML 表单(然后该表单允许我在 JSON 中推送新数据)。它可以工作,显示表单,但我没有提前考虑,也不知道如何从用户那里检索输入值。

 createNewLineForm() {
    if (this.allowForm) {
      let indiceList = 0;
      this.formOn = true;

      for (let jsonCol of this.columnData) {

        if (jsonCol.visible === true && jsonCol.type === "Boolean") {

          var content = document.getElementById("formcontent");

          var listTitle = document.createElement("header");
          listTitle.textContent = jsonCol.name;

          content.appendChild(listTitle);

          var selectList = document.createElement("select");
          selectList.id = "trueFalse";

          content.appendChild(selectList);

          var option = document.createElement("option");
          option.value = "true";
          option.text = "true";

          selectList.appendChild(option);

          var option2 = document.createElement("option");
          option2.value = "false";
          option2.text = "false";

          selectList.appendChild(option2)
        }

        if (jsonCol.visible === true && jsonCol.type === "String" && jsonCol.dropdownList === null) {

          var content = document.getElementById("formcontent");

          var inputTitle = document.createElement("header");
          inputTitle.textContent = jsonCol.name;


          content.appendChild(inputTitle);

          var formInput = document.createElement("input");
          formInput.type = "text";
          formInput.placeholder = jsonCol.name;
          formInput.name = "text";

          content.appendChild(formInput);

        }

        if (jsonCol.visible === true && jsonCol.type === "String" && jsonCol.dropdownList != null) {

          var content = document.getElementById("formcontent");

          var listTitle = document.createElement("header");
          listTitle.textContent = jsonCol.name;

          content.appendChild(listTitle);

          var selectListLong = document.createElement("select");
          selectListLong.name ="longList";

          indiceList++;

          content.appendChild(selectListLong);


          for (const options of jsonCol.dropdownList) {

            var option = document.createElement("option");
            option.value = options;
            option.text = options;

            selectListLong.appendChild(option);

          }
        }
      }
      var submit = document.createElement("input");
      submit.type = "submit";
      content.appendChild(submit);
    }
  }

然后在我的 HTML 窗口中使用简单的<div id="formcontent"> </div> 有条件地调用它

我尝试使用 document.getElementByName('...').value,似乎不起作用。

顺便说一句,如果您对如何制作该表格有其他想法,我也想听听,我认为这种方法似乎有点混乱。

【问题讨论】:

    标签: html json angular forms


    【解决方案1】:
    var content = document.getElementById("formcontent").value;
    

    你可以通过ID试试

    【讨论】:

    • 使用 getElementById("formcontent").value 给我错误“'HTMLElement'类型上不存在属性'value'”。我尝试使用 nodeValue,并没有改变任何东西。我所做的是我完成了表单,点击提交,然后按下一个调用此函数的按钮:retrieveForm(){ var content = document.getElementById("formcontent").value; console.log("contenu", content); } 我得到的只是内容为空......我这样做对吗?
    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 2021-10-15
    • 2015-06-03
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多