[英]How to dynamically generate HTML form including action and input and submit in JavaScript?
[英]Dynamically generate a form (html)?
我是javascript / jQuery的新手,我正在尝试学习如何动态创建表单。 阅读了一堆教程后,我得到了一些代码。 我在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;
}
这是jsfiddle: http : //jsfiddle.net/5pF8R/2/
问题是标签没有显示出来,我尝试用<p>替换标签,但是也没有显示出来。
任何对我做错事的帮助都会很棒。
非常感谢。
您想要这个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.