javascript - 如何根据用户选择下拉值创建动态字段
问题描述
我想要做的是在我下面的代码中,当我选择下拉列表值时,动态字段像文本字段选择字段等一样打开。当我更改下拉列表值时,新字段像上传文件字段一样打开文本字段等,但在我下面的代码中不起作用。
我们如何使用 HTML CSS JavaScript jQuery 做到这一点?
function addElement() {
var element = document.createElement("input");
element.setAttribute("value", "");
element.setAttribute("name", "newInput");
var foo = document.getElementById("newAdd");
foo.appendChild(element);
}
function addInput(){
document.getElementById("newAdd").innerHTML="";
var noInp=parseInt(document.getElementById("formName9").value);
while(noInp>0)
{
addElement();
noInp--;
}
}
<select id="formName9" name="blockUnits" onchange="return addInput()">
<option value="">select field</option>
<option value="4">apple four field</option>
<option value="2">banana two filed</option>
<option value="6">grapes six field</option>
<option value="1">apple 1 field</option>
<option value="4">guava four field</option>
<option value="8">lichi eight field</option>
</select>
<form>
<div id="newAdd"> </div>.
</form>
解决方案
document.getElementById("formName9").value
不是一个数字。您应该添加.length
以返回字符串中的字符数。
var noInp=parseInt(document.getElementById("formName9").value.length);
推荐阅读
- android - 失败:APK 构建失败
- flutter - Bloc - 是否可以在导航堆栈中为上一页产生状态?
- arrays - 使用 Excel 清除 VBA 中的对象数组的性能问题
- flutter - 使用带有 Flutter Hooks 的 video_player 包播放背景全屏视频
- amazon-web-services - AWS Athena Javascript SDK - 从查询结果创建表 (CTAS) - 指定 otuput 格式
- python - 恢复预定义变量作为输入
- angular - Angular 构建和服务器打开
- ruby-on-rails - 如何在 Active Record 中删除(解除关联)关系而不删除
- python-3.x - 需要帮助制作一个 discord.py 重写机器人,服务器将语音通道中的每个人都静音
- python - Python readlines() 在 for 循环中没有函数输出错误