简体   繁体   English

添加删除

[英]add remove <li with textbox on button click. (out of ul wrap problem) and name inputs uniquely

I have an issue with js. 我的js有问题。 I have form like below. 我有下面的表格。 Add button is adding li with input box and Remove button is removing... it is working very well. “添加”按钮正在与输入框一起添加li,而“移除”按钮正在被删除...。 the issue is, it is creating the li after ul, out of ul :/ you can see it clearly at the screenshot below. 问题是,它是在ul之后创建li,而在ul之后:/您可以在下面的屏幕截图中清楚地看到它。 any idea about solution? 关于解决方案有什么想法吗? appreciate!!! 欣赏!!! thanks!!! 谢谢!!!

* *

ps, how can I name the created inputs unique? ps,如何命名创建的输入唯一? now all has same name which is txt :/ 现在都具有相同的名称,即txt:/

* *


JS CODE JS代码

function addTextBox() {
    var form = document.contact;
    form.appendChild(document.createElement('li')).innerHTML = "Name <input type=\"text\" name=\"txt\" class=\"txt_input required\">";
}

HTML CODE: HTML代码:

                        <li id="persons_add"><label for="persons"># of Persons Attending: *</label>

                    <table width="40%" border="1">
                        <tr>
                            <td><input type="text" name="count" value="0" class="txt_input required" style="width:20px;"  readonly ></td>
                            <td><INPUT type="button" value="ADD" name="add" onClick="incrementCount()"></td>
                            <td><INPUT type="button" value="Remove" name="remove" onClick="decCount()"></td>
                        </tr>
                    </table>

                    </li>
                    <li class="alignRight"><input type="submit" value="Register" id="btnsend" name="btnsend" class="btn_submit" /></li>
                </ul>

SCREENSHOT of issue 发行画面

alt text http://xs1144.xs.to/xs1144/09434/buuu851.jpg 替代文字http://xs1144.xs.to/xs1144/09434/buuu851.jpg

Shouldn't you to append the "li" to the "ul" instead of the form ? 您不应该将“ li”附加到“ ul”而不是格式吗?

function addTextBox() {
    var myUl = document.getElementById('`___your_ul_id____`'); **<--- change this**
    myUl.appendChild(document.createElement('li')).innerHTML = "Name <input type=\"text\" name=\"txt\" class=\"txt_input required\">";
}

You need to append the new li to the ul, not the form element. 您需要将新li附加到ul,而不是form元素。

var ul = document.getElementById("ul_id");
ul.appendChild(document.createElement('li')).innerHTML = "Name <input type=\"text\" name=\"txt\" class=\"txt_input required\">";

在UL元素而不是form元素上调用appendChild。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM