【问题标题】:Dynamic add textbox input's will save to the database as one动态添加文本框输入将作为一个保存到数据库中
【发布时间】:2015-10-18 05:36:07
【问题描述】:

所以我有用于颜色的动态添加文本框,我想要的是如果用户输入颜色然后添加 2 次,例如蓝色然后红色然后黄色,所以如果用户提交它。它将以蓝色、红色、黄色的形式通过数据库。

这是带有添加文本框的javascript功能的代码

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementsByName('theValue');
  var num = (numi.length + 1);

  var newdiv = document.createElement('div');
  var divIdName = 'my' + num + 'Div';
  newdiv.setAttribute('id', divIdName);
  newdiv.setAttribute('name', 'theValue');
  newdiv.innerHTML = '<div class="form-group"><label for="color" class="control-label col-xs-4"><p class="left"></p></label> <div class="col-xs-7">  <input type=text id=' + num + 'value= ' + num + ' class= "req"><a class="btn btn-default bt" href="javascript:remove(' + divIdName + ')">Remove</a>';
  ni.appendChild(newdiv);
}

function remove(dId) {
  var ni = document.getElementById('myDiv');
  ni.removeChild(dId);
}
<label for="color" class="control-label col-xs-4">
    <p class="left">Color/s</p>
</label>
<div class="col-lg-1">
    <input name="color[]" class="req" id="theValue[]" autocomplete = "off" required/>
    <div id="myDiv"></div>
    <p><a class="btn btn-default bt " role="button" href="javascript:addElement()" >Add Color</a></p>
    <div style='clear: both;'></div>
</div>
</div>

【问题讨论】:

  • 你有什么问题? @Vista
  • 提交按钮在哪里

标签: javascript php jquery database


【解决方案1】:

我已尝试使代码与您已有的代码保持一致,但是,您可以采取其他措施来改进这一点,但这些会及时出现,即;使用诸如 knockout.js 和 html 模板之类的东西。

另外,我不知道您是如何将数据存储在数据库中的,因此将表单控件的名称/ID 与您将从存储中加载和保存的内容相匹配是另一个主题。

请注意,在删除项目时,不要减少 controlIndex - 它纯粹是为了唯一标识。如果你想更好地控制索引,那么数组(可能还有 knockout.js)可能会加入其中。

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <meta charset="utf-8" />
</head>

<body>
  <p class="left">
    <label for="color" class="control-label col-xs-4">Color/s</label>
  </p>
  <div class="col-lg-1">
    <div id="myDiv">
      <input name="color0" class="req" id="color0" autocomplete="off" required />
    </div>
    <p><a class="btn btn-default bt " role="button" href="javascript:addElement()">Add Color</a>
    </p>
    <div style='clear: both;'></div>
  </div>
  <script type="text/javascript">
    // Create an index for the control Ids
     // First one (and fixed on page) is 0 (color0).
    var controlIndex = 0;

     // Main Div for additional elements.
    var myDiv = document.getElementById("myDiv");

    function addElement() {
      // Next index.
      controlIndex++;
      // Create new Div for new elements to be grouped in.
      var newDiv = document.createElement("div");
      newDiv.id = 'colorDiv' + controlIndex;
      // Create new input element and set its properties.
      var newElement = document.createElement("input");
      newElement.id = newElement.name = 'color' + controlIndex;;
      newElement.class = 'req';
      newElement.setAttribute('required', 'required');
      newElement.setAttribute('autocomplete', 'off');
      // Create link to enable removal of new Div.
      var newRemovalLink = document.createElement("a");
      newRemovalLink.class = 'btn btn-default bt';
      newRemovalLink.setAttribute('href', "javascript:remove('" + newDiv.id + "')");
      newRemovalLink.innerHTML = ' X ';
      // Add the elements to the new Div and add that to the main Div.
      newDiv.appendChild(newElement);
      newDiv.appendChild(newRemovalLink);
      myDiv.appendChild(newDiv);
    }

    function remove(elementId) {
      // Get the main Div.
      var myDiv = document.getElementById('myDiv');
      // Get the Div (or other element) to remove.
      var element = document.getElementById(elementId);
      // Remove it from the main Div.
      myDiv.removeChild(element);
    }
  </script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2023-04-04
    • 2013-05-07
    • 1970-01-01
    相关资源
    最近更新 更多