![](/img/trans.png)
[英]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.