[英]Create dynamic div with jquery
Im trying to create a button that generates already formated divs everytime it is pushed. 我试图创建一个按钮,每次按下它时都会生成已经格式化的div。
The divs are composed by forms and their fields should already be filled with data that is stored in variables in javascript. div由表单组成,它们的字段应该已经填充了存储在javascript中的变量中的数据。
eg. 例如。
<div id="myDiv_#">
<form id="myForm">
<input type="text" id="start_date" name="start_date" value="someJavascriptVariable" />
<input type="text" id="end_date" name="end_date" value="someJavascriptVariable" />
<select name="type" id="type">
<option value="1">"someJavascriptVariable"</option>
<option value="2">"someJavascriptVariable"</option>
</select>
<input type="button" id="new_button" value="Show">
</form>
</div>
<script>
$('#button_push').click(function()
{
//creating myDiv_#
}
</script>
I was searching arround web, but never found good results :s Im asking you help with this, maybe a guide line or a start point. 我正在搜索网络,但从来没有找到好的结果:我问你这方面的帮助,可能是一个指导或起点。 The point is, everytime user push the button, a new div is created with parameters above. 关键是,每次用户按下按钮时,都会创建一个带有上述参数的新div。
Cheers 干杯
With markup as complex as that it's probably easier if have a hidden version of it which you can clone, amend attributes of then append to the page as required. 如果有一个复杂的标记,如果它有一个你可以克隆的隐藏版本可能更容易,修改属性然后根据需要附加到页面。
Something like this: 像这样的东西:
<div id="container"></div>
<div style="display: none;">
<div id="myDiv_#">
<form id="myForm">
<input type="text" id="start_date" name="start_date" value="someJavascriptVariable" />
<input type="text" id="end_date" name="end_date" value="someJavascriptVariable" />
<select name="type" id="type">
<option value="1">"someJavascriptVariable"</option>
<option value="2">"someJavascriptVariable"</option>
</select>
<input type="button" id="new_button" value="Show">
</form>
</div>
</div>
$('#button_push').click(function() {
var $div = $("#myDiv_\\#").clone(true); // keep events'
$div.attr('id', '').addClass('clone'); // example of amending attributes
$("#container").append($div); // append
});
You can easily replace the someJavascriptVariable
strings with the specific variable relevant to that clone instance too. 您也可以使用与该克隆实例相关的特定变量轻松替换someJavascriptVariable
字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.