How to nest dynamic delete and add input boxes in JQ

problem description

JQ how to nest dynamic delete and add input boxes, but click on the first layer to add and delete input boxes are both OK. Now the problem is to set up one layer.

related codes


< html >
< head >

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQinput</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<style>
    .spot{float: left;width:200px;height: 40px;}
</style>

< / head >
< body >

<input type="button" id="" value=""/>
<div id="spots">
    <input type="text" name="" placeholder="">
    <input type="button" id="add" name="add" value=""/>
    <input type="button" id="del" name="del" value=""/><br/>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("input-sharpadd").click(function () {
            $("div-sharpspots").append(
                "<div class="spot">" +
                "<input type="text" name="ckname" /> " +
                "<i class="fa fa-minus-circle" id="del" style="cursor:pointer;color:-sharpFF1F1F"></i>" +
                "</div>"
            )
                .find("i-sharpdel").click(function () {
                    $(this).parent().remove();
                });
        });
        $("input-sharpdel").click(function () {
            $("div-sharpspots").remove();
        });
    })
</script>

< / body >
< / html >

the result I hope is this

Apr.30,2021

New elements do not add id, through the class selector, otherwise the page will have multiple identical id, which will cause problems


answer your own questions!
use it directly, ha!
< html >
< head >

<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script language="javascript">
    var qus = new Array();
    function Question() {
        this.qno = qus.length;
        this.ono = 0;
        this.create = function (table) {
            var qstr = "";
            qstr += "<hr id=\"qu" + this.qno + "div\"><div id=\"qu" + this.qno + "\"><h2>" + (this.qno + 1) + "</h2>";
            qstr += "<input type=\"text\" name=\"questions[" + this.qno + "]\" value=\"questions[" + this.qno + "]\" />";
            qstr += "<div id=\"qu" + this.qno + "op\"><b>*  *</b>";
            qstr += "<br /><input type=\"text\" value=\"qu" + this.qno + "op" + this.ono + "\" id=\"qu" + this.qno + "op" + this.ono + "\"/>";
            qstr += "</div>";
            qstr += "<input type=\"button\" value=\"\" onclick=\"qus[" + this.qno + "].addOption()\"/>";
            qstr += "<input type=\"button\" value=\"\" onclick=\"qus[" + this.qno + "].delOption()\"/>";
            qstr += "</div>";
            table.innerHTML += qstr;
            //alert(qstr);
        }
        this.addOption = function () {
            this.onoPP;
            var opar = document.getElementById("qu" + this.qno + "op");
            opar.innerHTML += "<br id=\"qu" + this.qno + "op" + this.ono + "div\"/><input type=\"text\" value=\"qu" + this.qno + "op" + this.ono + "\" id=\"qu" + this.qno + "op" + this.ono + "\"/>";
            //alert(opar.innerHTML);
        }
        this.delOption = function () {
            if (this.ono > 0) {
                var quop = "qu" + this.qno + "op";
                var opx = "qu" + this.qno + "op" + this.ono;
                document.getElementById(quop).removeChild(document.getElementById(opx));
                document.getElementById(quop).removeChild(document.getElementById(opx + "div"));
                this.ono--;
            }
        }
    }

    function createQuestion() {
        var qu = new Question();
        qus.push(qu);
        qu.create(document.getElementById('table'));
    }

    function delQuestion() {
        if (qus.length > 0) {
            var qupr = "table";
            var qux = "qu" + (qus.length - 1);
            document.getElementById(qupr).removeChild(document.getElementById(qux));
            document.getElementById(qupr).removeChild(document.getElementById(qux + "div"));
            qus.pop();
        }
    }
</script>

< / head >
< body >

<div id="table"></div>
<br />
<hr />
<br />
<input type="button" onclick="createQuestion();" value="" />
<input type="button" onclick="delQuestion();" value="" />

< / body >
< / html >

put a finished product:

Menu