jQuery循环创建元素 [英] jquery loop to create elements
本文介绍了jQuery循环创建元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我对这个任务还没有运气,因此非常感谢您的帮助.
I have had no luck with this task so far so grateful for any help.
我有一个html表单,其中有一个小的选择菜单(1-10) 即
I have an html form, in which there is a small select menu (1-10) ie
<select>
<option value = '1'>1</option>
<option value = '2'>2</option>
...
<option value = '10'>10</option>
</select>
根据选择的值,我希望jquery创建(或删除)一定数量的输入文本框(具有不同的名称和ID). 例如,如果选择2,则将创建以下输入:
depending on what value is selected i would like jquery to create (or remove) that number of input text boxes (with different names and id's). eg if 2 was selected these inputs would be created:
<input type = 'text' name = 'name1' id = 'id1' />
<input type = 'text' name = 'name2' id = 'id2' />
毫无疑问,我期待您简单而优雅的解决方案! 安迪
i look forward to your no doubt simple and elegant solutions! andy
推荐答案
类似以下内容:
$('select').change(function() {
var num = parseInt($(this).val(), 10);
var container = $('<div />');
for(var i = 1; i <= num; i++) {
container.append('<input id="id'+i+'" name="name'+i+'" />');
}
$('somewhere').html(container);
});
这篇关于jQuery循环创建元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文