0

每当我单击 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>

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

4

5 回答 5

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");

                  });



}
于 2013-08-21T17:48:07.000 回答
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

于 2013-08-21T17:48:17.397 回答
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);
     });
  });
于 2013-08-21T17:55:18.247 回答
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");    
}

例子

于 2013-08-21T17:52:48.397 回答
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");

    });

});
于 2013-08-21T17:46:15.217 回答