1

如何循环输入类型文本,以便它可以拥有自己的唯一属性,例如。名称,价值。我的意思是,例如 name="text1", name="text2",。类似的东西。这是我的代码。谢谢 :)

<HTML>
    <HEAD>
    <TITLE></TITLE>
    <SCRIPT language="javascript">
    function add(type) {

        //Create an input type dynamically.
        var element = document.createElement("input");

        //Assign different attributes to the element.
        element.setAttribute("type", "text");
        element.setAttribute("value", "typhere");
        element.setAttribute("name", "txtbox");

        var btns = document.createElement("input");


        btns.setAttribute("type", "button" );
        btns.setAttribute("value", "delete");
        btns.setAttribute("name", "dlete");


        var foo = document.getElementById("fooBar");

        //Append the element in page (in span).

                foo.appendChild(element);
                foo.appendChild(btns);
                var br = document.createElement("br");
                foo.appendChild(br);

    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM>
    <H2></H2>

    <BR/>


    <INPUT type="button" value="Add" onclick="add(document.forms[0].value);"/>

    <span id="fooBar"><br/></span>

    </FORM>
    </BODY>
    </HTML>
4

3 回答 3

1
     var i=1;
   function add(type) {

    //Create an input type dynamically.
    var element = document.createElement("input");

    //Assign different attributes to the element    
   element.setAttribute("type", "text");
    element.setAttribute("value", "typhere"+i);
    element.setAttribute("name", "txtbox"+i);

    var btns = document.createElement("input");


    btns.setAttribute("type", "button" );
    btns.setAttribute("value", "delete"+i);
    btns.setAttribute("name", "dlete"+i);
    i++;

使用变量 i 来增加值。

于 2013-11-05T07:16:24.393 回答
1

你可以这样做:

var inputId = 0;

function add(type){
   // do the stuff you have to do with inputId
   // input.setAttribute("name", "text" + inputId); for example
   inputId++;
}

如果你不想污染全局命名空间,你可以这样做:

(function(window){
  var inputId = 0;

  window.InputManager = {
     add : function(type){
         // do tuff with inputId
         // input.setAttribute("name", "text" + inputId); for example
         inputId++;
     }
  };
})(window);

接着

<input type="button" value="Add" onclick="InputManager.add(document.forms[0].value)"/>
于 2013-11-05T07:32:57.467 回答
1

尝试这个 :

var  counter=1;
function add(type) {

    //Create an input type dynamically.
    var element = document.createElement("input");

    //Assign different attributes to the element.
    element.setAttribute("type", "text");
    element.setAttribute("value", "typhere"+counter);
    element.setAttribute("name", "txtbox"+counter);

    var btns = document.createElement("input");


    btns.setAttribute("type", "button" );
    btns.setAttribute("value", "delete");
    btns.setAttribute("name", "dlete");


    var foo = document.getElementById("fooBar");

    //Append the element in page (in span).

            foo.appendChild(element);
            foo.appendChild(btns);
            var br = document.createElement("br");
            foo.appendChild(br);
            counter++;

}

演示:http: //jsfiddle.net/kLJWW/

于 2013-11-05T07:33:31.507 回答