Javascript 使用下拉菜单生成动态文本框

Javascript 使用下拉菜单生成动态文本框,javascript,Javascript,您好,我是java脚本的新手。我编写了一段代码,通过选择下拉选项1生成一个文本框,通过选择下拉选项2生成两个文本框,但我在文本框中得到了值,如选择0和选择1。。。。这是我的密码 <html> <script type="text/javascript"> window.onload = function() { var select = document.getElementById("select");

您好,我是java脚本的新手。我编写了一段代码,通过选择下拉选项1生成一个文本框,通过选择下拉选项2生成两个文本框,但我在文本框中得到了值,如选择0和选择1。。。。这是我的密码

<html>
<script type="text/javascript">

        window.onload = function()
        {
            var select = document.getElementById("select");
            var texts = document.getElementById("texts");
            select.onchange = function()
            {
                var val = select.options[select.selectedIndex].value;
                texts.innerHTML = "";
                for(i=0; i < val; i++)
                {
                    texts.innerHTML += '<div><input type="text" name="t_' + i + '" value="select_' + i + '" /></div>';
                }
            }
        }

    </script>
<body>
    <form method="POST" action="connection.php">
Question:<br>
<textarea name="question" cols="35" rows="5"></textarea><br>
<select id="select" size="1">
<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>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<div id="texts"></div>
<input type="submit" name="Submit" value="Submit" >
</form>
</body>
</html> 

window.onload=函数()
{
var select=document.getElementById(“select”);
var text=document.getElementById(“文本”);
select.onchange=function()
{
var val=select.options[select.selectedIndex].value;
text.innerHTML=“”;
对于(i=0;i

1. 2. 3. 4. 5. 6. 7. 8. 9

请建议我不要在文本框中获取值。我不知道哪里出错了。请提前感谢

文本输入的“值”属性是文本框中的文本。如果不希望在文本框中显示,只需执行以下操作:

    texts.innerHTML += '<div><input type="text" name="t_' + i + '" /></div>';
text.innerHTML+='';

有一种比使用innerHTML更好的方法,但现在我觉得很懒。当您设置文本输入时,只需删除value=part,我已经在循环外部设置了innerHTML,因为设置innerHTML有点昂贵(处理),不应该在循环中使用

    window.onload = function()
    {
        var select = document.getElementById("select");
        var texts = document.getElementById("texts");
        select.onchange = function()
        {
            var val = select.options[select.selectedIndex].value,
            //set the innerHTML one time using ret (Array)
            ret=[];
            texts.innerHTML = "";
            for(i=0; i < val; i++)
            {
                // do not set value property:
                ret.push('select_' + i + ': <div><input type="text" name="t_' 
                  + i + '"/></div>');
            }
            // set innerHTML here
            texts.innerHTML = ret.join("");
        }
    }
window.onload=function()
{
var select=document.getElementById(“select”);
var text=document.getElementById(“文本”);
select.onchange=function()
{
var val=select.options[select.selectedIndex].value,
//使用ret(数组)一次性设置innerHTML
ret=[];
text.innerHTML=“”;
对于(i=0;i
检查这个Hello jack我应该把值保存在哪里然后???Ananth我得到了相同的输出我不希望值在文本框中,而是希望值在外面我不知道如何做。谢谢advance@ShashankCool更新了我的答案,因此在文本框显示select_1之前: