gpt4 book ai didi

jquery循环创建元素

转载 作者:行者123 更新时间:2023-12-03 22:12:00 25 4
gpt4 key购买 nike

到目前为止,我还没有成功完成这项任务,非常感谢您的帮助。

我有一个 html 表单,其中有一个小的选择菜单 (1-10)即

<select>
<option value = '1'>1</option>
<option value = '2'>2</option>
...
<option value = '10'>10</option>
</select>

根据选择的值,我希望 jquery 创建(或删除)该数量的输入文本框(具有不同的名称和 ID)。例如,如果选择 2,将创建这些输入:

<input type = 'text' name = 'name1' id = 'id1' />
<input type = 'text' name = 'name2' id = 'id2' />

我期待您无疑简单而优雅的解决方案!安迪

最佳答案

类似这样的事情:

$('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循环创建元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com