日期:2014-05-17  浏览次数:20403 次

JS动态添加标签并取值
大概意思就是我有两个下拉列表框,select1和select2

里边存的是数字1-9

我选中select1的值为3 则页面出现3个text然后向其text中输入值 单击按钮 取出text的值..

用JS   大神们有好例子么?

------解决方案--------------------
用js拼接text个数,比如页面放一个<label id='lb'></label>
根据所选select个数for循环lb.innerHtml+=<input type='text' id='text_自定义'/>,然后根据text的不同ID把所有自定义的text值放到一个隐藏的text中用逗号隔开,然后后台split去拿每个值
------解决方案--------------------
<body>
<input type="button" value="input取值" onclick="getInputsText();" />
<select id="curSelect" onchange="selectChanged();" >
<option></option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select></body>


 function selectChanged() {
        //父容器
        var inputParent = document.getElementsByTagName("body")[0];
        var select = document.getElementById("curSelect");
        var selectValue = select.options[select.selectedIndex].value;
        if (selectValue !== "") {
            for (var i = 0; i < selectValue; i++) {
                var input = document.createElement("input");
                inputParent.appendChild(input);
            }
        }
       
    }
    //input 取值
    function getInputsText() {
        //父容器
        var inputParent = document.getElementsByTagName("body")[0];