使用jQuery将新元素添加到dom(动态)时,缺少name属性

时间:2016-04-19 13:57:38

标签: javascript jquery

我有这个JavaScript函数(下面),它会动态地向DOM添加一个新元素(没问题)除了没有添加名称属性或ID值。

我希望实现的最终目标是能够动态地向流程添加元素,然后能够保存它(通过jQuery序列化提交)而无需刷新页面(简单来说,考虑添加/删除任务到项目)。动态地“添加”是我正在努力的目标。

这是我到目前为止的精简代码:

<div id="itemList">
    <div><input class="textInput" name="input_1"  id="input_1" type="text" /></div>
    <div><input class="textInput" name="input_2"  id="input_2" type="text" /></div>
    <div><input class="textInput" name="input_3"  id="input_3" type="text" /></div>
</div>
<div><a href="javascript:void('');" id="addNewInputField" onClick="addNewInputField();">Add New Task</a></div>

<script type="text/javascript">
function addNewInputField(){
    elParentContainer = document.getElementById('itemList');
    newElement = document.createElement('div');
    newInput = document.createElement('input');
    newInput.setAttribute('class', 'textInput');
    newInput.setAttribute('type', 'text');      
    newElement.appendChild(newInput);
    elParentContainer.appendChild(newElement);
}
</script>

这是我在点击“添加新任务”两次时获得的输出源:

Here is the what I'm currently getting when you view source:

<div id="itemList">
    <div><input class="textInput" name="input_1" id="input_1" type="text"></div>
    <div><input class="textInput" name="input_2" id="input_2" type="text"></div>
    <div><input class="textInput" name="input_3" id="input_3" type="text"></div>
    <div><input class="textInput" type="text"></div>
    <div><input class="textInput" type="text"></div>
</div>

查看来源时,以下是所需输出:

<div id="itemList">
    <div><input class="textInput" name="input_1" id="input_1" type="text"></div>
    <div><input class="textInput" name="input_2" id="input_2" type="text"></div>
    <div><input class="textInput" name="input_3" id="input_3" type="text"></div>
    <div><input class="textInput" name="input_4" id="input_4" type="text"></div>
    <div><input class="textInput" name="input_5" id="input_5" type="text"></div>
</div>

有人可以帮我修改我的功能以增加新添加的DOM元素吗?

7 个答案:

答案 0 :(得分:2)

您可以考虑更新您的函数,以查看当前有多少元素(通过getElementsByClassName()函数),并在创建新元素时分别设置idname属性元素:

<script type="text/javascript">
  function addNewInputField(){
      // Set how many elements you have with that class (to determine which
      // value to append to 'input_{x}'
      var currentInput = document.getElementsByClassName('textInput').length + 1;
      elParentContainer = document.getElementById('itemList');
      newElement = document.createElement('div');
      newInput = document.createElement('input');
      newInput.setAttribute('class', 'textInput');
      // Set your new ID attribute
      newInput.setAttribute('id', 'input_' + currentInput);
      // Set your new name attribute
      newInput.setAttribute('name', 'input_' + currentInput); 
      newInput.setAttribute('type', 'text');      
      newElement.appendChild(newInput);
      elParentContainer.appendChild(newElement);
  }
</script>

您可以see a working example here以及输出标记在下面的示例:

enter image description here

答案 1 :(得分:2)

根据您的精简版本,您可以跟踪您拥有的输入数量,并在每次新添加时更新它们。这是基于您提交的代码的解决方案。

ForAll(xx, Implies(And(xx >= 0, xx <= x*60*60), And(func1(xx) >= 1, func1(xx) <= 100)))

   <div id="itemList">
<div><input class="textInput" name="input_1"  id="input_1" type="text" /></div>
<div><input class="textInput" name="input_2"  id="input_2" type="text" /></div>
<div><input class="textInput" name="input_3"  id="input_3" type="text" /></div>

答案 2 :(得分:1)

如果您想使用jQuery,请考虑使用this之类的代码:

  var inputCount = $('.textInput').size();
  inputCount += 1;
  $('#itemList').append('<div><input class="textInput" name="input_'+inputCount+'" id="input_'+inputCount+'" type="text" /></div>');

此外,使用数据属性可能更合适,您可能会考虑它。

答案 3 :(得分:1)

您可以计算容器中有多少个子项,然后动态创建下一个元素的ID和名称。

function addNewInputField(){
elParentContainer = document.getElementById('itemList');
var next_input = elParentContainer.children.length+1
newElement = document.createElement('div');
newInput = document.createElement('input');
newInput.setAttribute('class', 'textInput');
newInput.setAttribute('type', 'text');      
newInput.setAttribute('name', 'input_'+next_input);      
newInput.setAttribute('id', 'input_'+next_input);   
newElement.appendChild(newInput);
elParentContainer.appendChild(newElement);

}

答案 4 :(得分:1)

您可以跟踪当前的输入数量。这样,在创建新输入时,可以更轻松地设置所需的新输入属性。

     <script type="text/javascript">
        var inputCounter =  3;
     function addNewInputField(){
        var name = "input_";
        inputCounter= inputCounter+1;
        name = name.concat(inputCounter);
        elParentContainer = document.getElementById('itemList');
        newElement = document.createElement('div');
        newInput = document.createElement('input');
        newInput.setAttribute('class', 'textInput');
        newInput.setAttribute('type', 'text');
        newInput.setAttribute('name', name);
        newInput.setAttribute('id', name);      
        newElement.appendChild(newInput);
        elParentContainer.appendChild(newElement);
    }

   </script>

答案 5 :(得分:0)

实现此目的的一种方法是检索集合的最后一个子节点,然后使用其中一个属性来了解当前索引。检索完成后,可以轻松地将具有正确内容的属性添加到新创建的元素中:

function addNewInputField(){
    var elParentContainer = document.getElementById('itemList');

    //Get last child of list
    var inputList = elParentContainer.getElementsByTagName('input');
    var lastChild = inputList[inputList.length-1];

    //Get last index from the name attribute of last child
    var lastIndex = lastChild.getAttribute('name').split('_')[1];

    var newElement = document.createElement('div');
    var newInput = document.createElement('input');
    newInput.setAttribute('class', 'textInput');
    newInput.setAttribute('type', 'text');
    //Add new attributes to the newly created element
    newInput.setAttribute('name', 'input_'+(lastIndex++));
    newInput.setAttribute('id', 'input_'+(lastIndex++));   
    newElement.appendChild(newInput);
    elParentContainer.appendChild(newElement);
}

答案 6 :(得分:0)

您是否尝试过Jquery.append()功能。

希望这可以解决您的问题,您可以使用它来创建动态元素,并在页面中附加任何文本。

请参阅示例并尝试此链接,您将会有更好的想法。

http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_html_append_ref

如果需要更多支持,请告诉我。

谢谢, Bhuvan