首页 > 解决方案 > 如何根据用户选择下拉值创建动态字段

问题描述

我想要做的是在我下面的代码中,当我选择下拉列表值时,动态字段像文本字段选择字段等一样打开。当我更改下拉列表值时,新字段像上传文件字段一样打开文本字段等,但在我下面的代码中不起作用。

我们如何使用 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>

标签: javascripthtmljquery

解决方案


document.getElementById("formName9").value不是一个数字。您应该添加.length以返回字符串中的字符数。

  var noInp=parseInt(document.getElementById("formName9").value.length);

推荐阅读