javascript: changing the name attribute dynamically

╄→гoц情女王★ 提交于 2019-12-23 21:36:52

问题


I have this script I'm working on and there's no errors on it but I want to add some functions on it like when I click the button it adds but I want the name attribute of the input text to be changed too.

Here's my script:

javascript:

var a = 1;
function add() {

    var fContent = document.getElementById('1');
    var sContent = document.getElementById('2');
    if (a <= 10) {
        a++;
        var objTo = document.getElementById('m');
        var divtest = document.createElement("div");
        divtest.innerHTML = (sContent.innerHTML + a + fContent.innerHTML);
        alert(divtest.innerHTML); 
        objTo.appendChild(divtest);
    }
}

html:

<input type="button" onclick="add();" value="+" />
<div id="m">
<div id="1">
<input type="text" name="f">
<input type="text" name="l">
<input type="text" name="m">
</div>
<div id="2"></div>
</div>

OUTPUT:

2
<input type="text" name="f">
<input type="text" name="l">
<input type="text" name="m">

EXPECTED OUTPUT:

2
<input type="text" name="f2">
<input type="text" name="l2">
<input type="text" name="m2">

and so on...


回答1:


You're not doing anything to change the name attributes. Trying to make those changes with html concatenation will get you into trouble. This will get you started:

(function() {

  var a = 1;

  // get a reference to the container
  var container = document.getElementById("m");
  // get a reference to the first row of input
  var base = container.children[0];  
  
  document.querySelector("button").addEventListener("click", function(e) {

    if(++a > 10) return;
    
    // clone the first row of input
    var clone = base.cloneNode(1);
    
    // change the number text by setting the span's textContent
    clone.children[0].textContent = a;
    // set the names of the input fields
    clone.children[1].name = "f" + a;
    clone.children[2].name = "l" + a;
    clone.children[3].name = "m" + a;
    
    // add the new row to the container
    container.appendChild(clone);
    
    console.log(clone);

  });

})();
<button type="button">+</button>
<div id="m">
  <div><span>1</span><input type="text" name="f1"><input type="text" name="l1"><input type="text" name="m1"></div>
</div>

If you'd rather create the elements from scratch...

(function() {

  var a = 1;

  // get a reference to the container
  var container = document.getElementById("m");    
  var input;
  var span;
  var div;
  
  document.querySelector("button").addEventListener("click", function(e) {

    if(++a > 10) return;
    
    // create our div
    div = document.createElement("div");
    
    // create and append our span
    span = document.createElement("span");
    span.textContent = a;
    div.appendChild(span);
    
    // create and append inputs    
    ["f","l","m"].forEach(function(n){
       input = document.createElement("input");
       input.name = n + a;
       div.appendChild(input);            
    });
                
    // append our div
    container.appendChild(div);
    
    console.log(div);

  });

})();
<button type="button">+</button>
<div id="m">
  <div><span>1</span><input type="text" name="f1"><input type="text" name="l1"><input type="text" name="m1"></div>
</div>


来源:https://stackoverflow.com/questions/30039442/javascript-changing-the-name-attribute-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!