InnerHTML在添加多个文本框时擦除值

时间:2013-11-15 05:29:15

标签: javascript php html

我正在关注this link

添加动态文本框。但我的问题是,当我在第二个文本框中插入值,然后按下一个文本框的按钮时,第二个框中的值将被删除。所以我不能使用提交表格。有没有办法解决它?

HTML

<table style="width:500px;height:auto;">
<tr>
<th>Code-1</th>
<td><input type="text" name="code1" id="code"></td><td><input type="button" value="Add New Code" onClick="insert()"></td>
</tr>
<tr>
<th>Custom Markup-1</th><td>
<input type="text" name="markup1" id="markup"></td></tr>
<tr><th>Vendor-1</th><td>
<select id="v" name="v1">
<option value="0">Please Select..</option>
<?
while($v= mysql_fetch_array($vendor))
{
    echo "<option value=".$v['id'].">".$v['name']."</option>";
}
?>
</select></td>
</tr>
</table>

JS

<script language="javascript">
var i = 1;
function insert()
{
new_div.innerHTML = new_div.innerHTML +"<tr><th>Code-"+(i+1)+"</th><td><input type='text' id='code"+(i+1)+"' name='code"+(i+1)+"'></td></tr>";
new_div.innerHTML = new_div.innerHTML +"<tr><th>Custom Markup-"+(i+1)+"</th><td><input type='text' name='markup"+(i+1)+"'/></td></tr>";
new_div.innerHTML = new_div.innerHTML +"<tr><th>Vendor-"+(i+1)+"</th><td><select id='v'+i name='v"+(i+1)+"'><option value='0'>Please Select..</option><?php while($v= mysql_fetch_array($vendorInfo)){?><option value='<?php echo $v['id']; ?>'><?php echo $v['name']; ?></option><?php }?></select></td></tr>";
new_div.innerHTML = new_div.innerHTML +"<tr><td><hr></td><td><hr></td></tr>";
//document.getElementById('code2').value=document.getElementById('code2').value;
i++;
}
</script>

3 个答案:

答案 0 :(得分:1)

试试这个:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script>
            var txtBId=1;
            function addTB(){
                var div=document.getElementById("tbSet");
                var txt=document.createElement("input");
                txt.setAttribute("type","text");
                txt.setAttribute("id","tb"+(txtBId++));  
                // div.innerHTML=div.innerHTML +"<br>";    <------------------  
                var br=document.createElement("br");
                div.appendChild(br);         
                div.appendChild(txt);
            }
        </script>
    </head>
    <body>
        <form id="myform">

            <div id="tbSet">

            </div>
            <input type="button" value="New TextBox" onclick="addTB();">
            <input type="submit" value="Submit">

        </form>

    </body>
</html>

答案 1 :(得分:0)

Donot通过innerHtml添加文本框。如果添加它,将删除3个数据。做它是createElement,然后添加孩子。

 function addTB(){
                var div=document.getElementById("tbSet");
                var txt=document.createElement("input");
                txt.setAttribute("type","text");
                txt.setAttribute("id","tb"+(txtBId++));                
                var br=document.createElement("br");
                div.appendChild(br);         
                div.appendChild(txt);
     }

答案 2 :(得分:0)

对于下拉:

var slId=1;
            function options(name,value){

                 var o=document.createElement("option");
                 o.setAttribute("value",value);
                 o.innerHTML=name;
                 return o;

            }
            function addDropDown(){
                  var div=document.getElementById("tbSet");
                var sl=document.createElement("select");                 
                 var o=options("Hello","hi");            
                 sl.setAttribute("id","sl"+(slId++));
                 sl.appendChild(o);                 
                 div.appendChild(sl);
            }