我是javascript / jQuery的新手,我正在努力学习如何动态创建表单。在阅读了一堆教程之后,我得到了一些代码。我把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/
问题是标签没有显示,我试图用
替换标签,但这也没有出现。
对我做错事的任何帮助都是很好的。
非常感谢。
发布于 2014-01-13 22:57:19
你想要这个演示
function createQuestion(){
$('<div/>',{
id: 'questionNumber',
class: 'form-group',
html: createQuestionContent()
}).appendTo('#question-container');
}
function createQuestionContent(){
return '<label> Enter the first question </label>';
}https://stackoverflow.com/questions/21102903
复制相似问题