gpt4 book ai didi

javascript - 输入数字并根据输入的数字显示输入字段

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

我想根据输入的数量来显示输入框的数量。

我的代码无法运行

<input id="howmany" />
<div id="boxquantity"></div>

Jquery/JavaScript

$(function() {
$('#howmany').change(function(){
for(i=0; i < $("#howmany").value; i++)
{
$('#boxquantity').append('<input name="boxid[]" type="file" id="boxid[]" size="50"/>');
}
});
});

最佳答案

我认为代码的问题是它在 jquery 对象上使用 .value 。我将 $("#howmany").value 替换为 $("#howmany").val()

我还添加了一个删除函数来清除显示的输入数量。

请运行代码片段,谢谢

$(document).ready(function() {
$('#howmany').change(function() {
$("#boxquantity input").remove();

for (i = 0; i < $("#howmany").val(); i++) {
$('#boxquantity').append('<input name="boxid['+i+']" type="file" id="boxid['+i+']" size="50"/>');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="howmany" />
<div id="boxquantity"></div>

关于javascript - 输入数字并根据输入的数字显示输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372570/

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