1. 程式人生 > >Jquery 實現動態添加輸入框&編號

Jquery 實現動態添加輸入框&編號

del 效果圖 3.3 src i+1 var remove 添加 http

輸入框動態增加和刪除並重新編號:

代碼附上:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加元素</title>
    <script src="jquery-3.3.1.js"></script>
    <script>
        var i = 1;
        $(function () {
            $("#box").on(‘click‘,‘.addp‘,function () {
                //$(‘#pp‘).after($("#pp").clone());
                var addP = $(this).parent().clone();
                $(this).parent().after(addP);
                $(‘#pp a‘).each(function (i,e) {
                    $(e).text(i+1);
                })
            });
            $("#box").on("click",‘.delp‘,function () {
                $(this).parent().remove();
                $(‘#pp a‘).each(function (i,e) {
                    $(e).text(i+1);
                })
            })
        });

    </script>
</head>
<body>
<div id="box"><p id="pp"><a>1</a><input type="text" name="gx"><button class=‘addp‘>+</button><button class=‘delp‘>-</button></p></div>
</body>
</html>

  效果圖:

技術分享圖片

Jquery 實現動態添加輸入框&編號