简体   繁体   English

动态地将一些文本和文本框添加到现有网页

[英]Dynamically add some text and text boxes to existing web page

I am trying to create a number of text boxes with specific captions in front of each text box, dynamically. 我试图动态地在每个文本框的前面创建许多带有特定标题的文本框。

I want the actual jquery code to create the textboxes and labels to lie within the following for loop--- 我希望实际的jquery代码创建文本框和标签,使其位于以下for循环内-

function addelements(){

    jQuery('<form action="test" id="data-form" name="data-form" method="POST" onsubmit="senddatafinal();">  <p> This is a new form </p><br />First name: <input type="text" name="firstname" /><br />Last name: <input type="text" name="lastname" />').appendTo('body');
    for(i=0; i<=3; i++)
    {      
         textmessage= "This is element # " + (i+1);
         fieldname= "field_" + (i+1);
     //now add code to show label stored in 'textmessage' 
     //to the form 'data-form'
     //now add code to show text box with name stored in 'fieldname' 
     //to the form 'data-form'
    }
}

您始终可以使用表格使其变得简单。

$('[id$=yourtable] > tbody:last').append('<input...>Label');

Found the below code (slightly modified it for my requirements) at http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/ http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/找到了以下代码(针对我的要求进行了稍微修改)

Code---- 码 - -

<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>Textbox #'+ counter + ' : </label>' +
      '<input type="text" name="textbox' + counter + 
      '" id="textbox' + counter + '" value="" >');

newTextBoxDiv.appendTo("#TextBoxesGroup");


counter++;
 });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM