如何动态获取两个文本框?

时间:2013-08-21 17:41:58

标签: javascript jquery

每当我点击addtextboxes按钮时,我想有两个文本框,我使用此代码生成单个文本框,它工作正常。

<script type="text/javascript">
$(document).ready(function () {
    var counter = 1;

    $("#addButton").click(function () {

        var newTextBoxDiv = $(document.createElement('div'))
            .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.after().html('<label>answer_' + counter + ' : </label>' + '<input type="text" name="answer_' + counter + '" id="answer_' + counter + '"  value="" >');
        newTextBoxDiv.appendTo("#TextBoxesGroup");
        counter++;
    });

});
</script>

<div id='TextBoxesGroup'>
    <div id="TextBoxDiv1">
        <label>answer #1 : </label><input type='textbox' id='answer1' >
    </div>
</div>

这里我想知道当我点击addbutton时我怎么能有两个文本框? 任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:1)

使用for循环两次并在循环中移动代码。

for (var i=0 ;i<=counter ;i++){


var newTextBoxDiv = $(document.createElement('div'))
                       .attr("id", 'TextBoxDiv' + i);

                        newTextBoxDiv.after().html('<label>answer_'+ i+ ' : </label>' + '<input type="text" name="answer_' + counter + '" id="answer_' + i+ '"  value="" >'  );
                        newTextBoxDiv.appendTo("#TextBoxesGroup");

                  });



}

答案 1 :(得分:1)

$("#addButton").click(function () {

    var counter = 1;
    for (var i=0;i <= counter;i++)
    {
        var newTextBoxDiv = $(document.createElement('div'))
                   .attr("id", 'TextBoxDiv' + i);
        newTextBoxDiv.after().html('<label>answer_'+ i + ' : </label>' + '<input type="text" name="answer_' + i + '" id="answer_' + i + '"  value="" >'  );
        newTextBoxDiv.appendTo("#TextBoxesGroup");
    }
});

如果您想要更多textboxes。增加counter value

答案 2 :(得分:0)

$(document).ready(function () {
    var counter = 1;

    $("#addButton").click(function () {

        var newTextBoxDiv = $(document.createElement('div'))
            .attr("id", 'TextBoxDiv' + counter);

        var $html = '<label>answer_' + counter + ' : </label>' + '<input type="text" name="answer_' + counter + '" id="answer_' + counter + '"  value="" >';
        counter++;
        $html += '<label>answer_' + counter + ' : </label>' + '<input type="text" name="answer_' + counter + '" id="answer_' + counter + '"  value="" >';
        counter++;
        newTextBoxDiv.after().html($html);
        newTextBoxDiv.appendTo("#TextBoxesGroup");

    });

});

答案 3 :(得分:0)

通过点击事件的内部工作并将其置于自己的功能中,您可以执行以下操作:

<强> HTML

<button id="addButton">Add</button>
<div id='TextBoxesGroup'></div>

<强>的JavaScript

var counter = 1;
var labelArray = ["answer", "order"];

$("#addButton").click(function () {
    for(var i = 0; i < labelArray.length; i++){
        createNewInput(labelArray[i]);
    }
    counter++;
});

function createNewInput(label){
    var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
    newTextBoxDiv.after().html('<label>'+ label + " " + counter + ' : </label>' + '<input type="text" name="answer_' + counter + '" id="answer_' + counter + '"  value="" >'  );
    newTextBoxDiv.appendTo("#TextBoxesGroup");    
}

EXAMPLE

答案 4 :(得分:0)

试试这个:

  $(document).ready(function(){
     var counter = 1;
       $("#addButton").click(function () {
          var clone1 = $("#TextBoxDiv1").clone();
          clone1.children('input').attr('id',"answer_"+counter)  ;
          clone1.children('input').attr('name',"answer_"+counter);
           $("#TextBoxesGroup").append(clone1);
          counter++;
          var clone2 = $("#TextBoxDiv1").clone();
          clone2.children('input').attr('id',"answer_"+counter)  ;
          clone2.children('input').attr('name',"answer_"+counter);
          $("#TextBoxesGroup").append(clone2);
     });
  });