简体   繁体   English

动态生成表格(html)?

[英]Dynamically generate a form (html)?

I am new to javascript / jQuery and I am trying to learn on how to create a form dynamically. 我是javascript / jQuery的新手,我正在尝试学习如何动态创建表单。 After reading a bunch of tutorials I have got some code. 阅读了一堆教程后,我得到了一些代码。 I have jQuery loaded in the head tag. 我在head标签中加载了jQuery。

//HTML
<body> 
<div class="container row-margin-top" id="question-container">
<button class="btn btn-default" type="button" onClick="javascript:createQuestion();">     
Create A Question </button>
</div>
</body>

//JavaScript
function createQuestion(){
var questionDiv = $(document.createElement('div'));
questionDiv.attr("id","questionNumber");
questionDiv.addClass("form-group");
questionDiv.html(function() {
    createQuestionContent();
});
$('#question-container').append(questionDiv);
    }

    function createQuestionContent(){
var html = "";
    html += '<label> Enter the first question </label>';
return html;
   }

Here is the jsfiddle : http://jsfiddle.net/5pF8R/2/ 这是jsfiddle: http : //jsfiddle.net/5pF8R/2/

The problem is that the label is not showing up, I tried to replace the label with < p > but that did not show up either. 问题是标签没有显示出来,我尝试用<p>替换标签,但是也没有显示出来。

Any help on what I am doing wrong would be great. 任何对我做错事的帮助都会很棒。

Thank You So much. 非常感谢。

You want this DEMO : 您想要这个DEMO

function createQuestion(){
  $('<div/>',{
    id: 'questionNumber',
    class: 'form-group',
    html: createQuestionContent()
  }).appendTo('#question-container');
}

function createQuestionContent(){
return '<label> Enter the first question </label>';
}

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

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