【问题标题】:Submit button issue提交按钮问题
【发布时间】:2021-01-03 17:48:33
【问题描述】:

我怎样才能有一个提交按钮,它实际上是在表单中并提交数据,这里的问题是如果我在循环中移动提交按钮,它会出现每个请求的输入字段,我怎样才能停止它并且只有一个输入字段末尾的提交按钮。

const div = document.querySelector(".addHere");

document.querySelector(".btn").addEventListener("click", addInputs);

function addInputs() {

const inputValue = parseInt(document.querySelector(".inputValue").value);

  if (isNaN(inputValue)) {
    alert("Wrong input");
  } else {
    for (let i = 1; i <= inputValue; i++) {
      const form = document.createElement("form");
      form.method = "post";
      form.action = "#";

      const input1 = document.createElement("input");
      input1.type = "text";
      input1.maxLength = "12";
      input1.className = "factor";
      input1.required = true;

      const input2 = document.createElement("input");
      input2.type = "text";
      input2.maxLength = "1";
      input2.className = "priority";
      input2.required = true;

      const br = document.createElement("br");

      form.appendChild(br.cloneNode());
      form.appendChild(input1);
      form.appendChild(input2);
      form.appendChild(br.cloneNode());

      div.appendChild(form);
    }

    const sub = document.createElement("button");
    sub.type = "submit";
    sub.value = "Submit";
    sub.className = "subButton";
    sub.textContent = "Submit";

    div.appendChild(sub);
  }
}
<div class="addHere"></div>
<div class="inputs">
  <input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
  <button class="btn">+</button>
</div>

【问题讨论】:

  • 我给你做了一个 sn-p 并修正了你的错字
  • 我建议不要生成这样的表格。还有一个错字,是的,将输入值放入该函数
  • @ptts 我应该如何动态生成表单?
  • 一定要动态生成吗?我只是将它编码为 HTML,甚至谷歌自己也使用 HTML 来制作非常漂亮的 Materialise UI。加上用户输入,我假设这不会进入数据库或任何东西,而是尽快学习服务器端语言以验证用户输入,只需跳入其中,php 和 nodejs 与 express 是富有成效的,周围没有太多庆祝:-)
  • @ptts 是的,我想使用 js 动态生成它,因为我想让用户决定它想要多少输入,一旦我掌握了 vanilla js 本身,我就会学习 nodejs,好吧你有什么建议可以改进吗?

标签: javascript loops submit-button


【解决方案1】:
  • 将提交按钮放在 HTML 中
  • 使用 CSS :empty 确定元素是否没有后代。如果为空,则使用#forms:empty ~ #submit { display: none; } 隐藏按钮提交

// UTILITY FUNCTIONS:
const ELNew = (sel, attr) => Object.assign(document.createElement(sel), attr || {});
const ELS   = (sel, EL) => (EL || document).querySelectorAll(sel);
const EL    = (sel, EL) => (EL || document).querySelector(sel);

// APP:
const EL_forms = EL("#forms");
const EL_value = EL("#value");
const EL_add   = EL("#add"); // be more specific, add IDs to your elements

EL_add.addEventListener("click", addInputs);

const customForm = () => {
  const form = ELNew("form",  {method:"post", action:"#"});
  const inpA = ELNew("input", {type:"text", maxLength:"12", className:"factor", required:true});
  const inpB = ELNew("input", {type:"number", maxLength:"1", className:"priority", required:true});
  form.append(inpA, inpB);
  return form;
}

function addInputs() {
  const times = parseInt(EL_value.value, 10);
  if (isNaN(times)) return alert("Wrong input");
  const DF_forms = [...Array(times)].reduce((DF) => (DF.append(customForm()), DF), new DocumentFragment);
  EL_forms.append(DF_forms);
  EL_value.value = ""; // Reset input value
}
#forms:empty ~ #submit { display: none; }
#forms form {padding-bottom: 5px;}
<div id="forms"></div>
<input type="text" maxlength="1" id="value" placeholder="Insert number:" />
<button id="add" class="btn">+</button><br>
<button id="send" type="button" value="Submit">Submit</button>

如果你想用 JS 来切换你的按钮可见性:

/* UTILITY */
.u-none {display:none !important;}
EL("#send").classList.toggle("u-none", !EL("form", EL_forms));

【讨论】:

  • 先生,我是编程新手,对您编写代码的方式印象深刻,我想要一些关于如何编写这样的 javascript 的提示,您的代码是用函数式编程范式编写的吗?
  • 我建议不要对任何名称或 ID 使用保留字 submit。如果名称或 ID 提交的元素在表单中,则表单立即失去脚本提交的能力。
  • 这段代码看起来像是在重新发明 jQuery... 不确定我是否明白这一点。如果 OP 不能理解您的代码,则尤其如此。
  • 谢谢指出 submit 怪癖@mplungjan
【解决方案2】:
  • 正确拼写功能
  • 将 const inputValue 移到函数内部
  • 将表单移到循环之外
  • 仅当提交不存在时才创建。

const div = document.querySelector(".addHere");

document.querySelector(".btn").addEventListener("click", addInputs);

function addInputs() {
  const inputValue = parseInt(document.querySelector(".inputValue").value);
  if (isNaN(inputValue)) {
    alert("Wrong input");
    return;
  }
  const form = document.createElement("form");
  form.method = "post";
  form.action = "#";
  for (let i = 1; i <= inputValue; i++) {
    const input1 = document.createElement("input");
    input1.type = "text";
    input1.maxLength = "12";
    input1.className = "factor";
    input1.required = true;

    const input2 = document.createElement("input");
    input2.type = "text";
    input2.maxLength = "1";
    input2.className = "priority";
    input2.required = true;

    const br = document.createElement("br");

    form.appendChild(br.cloneNode());
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(br.cloneNode());

    div.appendChild(form);
  }

  let sub = document.getElementById("sub") || document.createElement("button");
  if (!sub.id) { // it is a new button
    sub.id = "sub";
    sub.type = "submit";
    sub.value = "Submit";
    sub.className = "subButton";
    sub.textContent = "Submit";
  }
  form.appendChild(sub); // moves if exists

}
<div class="addHere"></div>
<div class="inputs">
  <input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
  <button class="btn">+</button>
</div>

【讨论】:

  • 先生,您想详细说明您创建的子按钮,它的工作方式和时间,以及为什么 if 循环围绕它的属性,谢谢。
  • 我将现有按钮分配给一个变量。如果按钮不存在,OR (||) 将分配一个新按钮。如果 sub 有一个已经存在的 ID - 如果没有,它是一个新按钮,所以我将它添加到 DOM
猜你喜欢
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多