• js 动态添加删除demo


    最近做了个需求,如下图

     点击加号可以添加一项,点击减号删除这项,实现起来相对来说简单

    html代码:

                             <div class="mui-input-row otherbankName">
                                <label style="font-size: 13px">其他银行卡开户行</label>
                                <input type="text" class="otherbankNameinput" placeholder="填写其他银行卡开户行">
                                <div class="img">
                                    <img class="triangleaddicon" src="../image/addicon.png" alt="">
                                </div>
                            </div>
                            <div class="mui-input-row otherbankNum">
                                <label style="font-size: 13px">其他银行卡账号</label>
                                <input type="text" class="otherbankNuminput" placeholder="填写其他银行卡账号">
                                <div class="img">
                                    <img class="triangleaddicon" src="../image/addicon.png" alt="">
                                </div>
                            </div>

    js代码:当点击的时候在页面创建一个div并且插入在加号div的下面,

    function addbanknum (){
        var timestamp = parseInt((new Date()).valueOf());
        var div = document.createElement("div");
        div.className = "mui-input-row otherbankNum";
        div.id = "otherbankNum"+ timestamp;
        div.innerHTML = `
        <label style="font-size: 13px">其他银行卡账号</label>
        <input type="text" class="otherbankNuminput" placeholder="填写其他银行卡账号">
        <div class="img" id="triangleminusicon${timestamp}" onclick="delefun1(${timestamp})">
             <img class="triangleminusicon" src='../image/minus.png' alt="">
        </div>
        `
        var otherbankNum = document.querySelector('.otherbankNum')
        document.getElementById("baseinfocontent").insertBefore(div,otherbankNum.nextSibling);
    }
    //添加银行卡账号
    mui(".otherbankNum .img")[0].addEventListener('tap', function () {
        addbanknum ()
    })
    
    function addbankname () {
        var timestamp = parseInt((new Date()).valueOf());
        var div = document.createElement("div");
        div.className = "mui-input-row otherbankName";
        div.id = "otherbankName" + timestamp;
        div.innerHTML = `
        <label style="font-size: 13px">其他银行卡开户行</label>
        <input type="text" class="otherbankNameinput" placeholder="填写其他银行卡开户行">
        <div class="img" id="triangleminusicon${timestamp}" onclick="delefun2(${timestamp})">
             <img class="triangleminusicon"  src='../image/minus.png' alt="">
        </div>
        `
        var otherbankName = document.querySelector('.otherbankName')
        document.getElementById("baseinfocontent").insertBefore(div,otherbankName.nextSibling);
    }
    //添加银行卡开户行
    mui(".otherbankName .img")[0].addEventListener('tap', function () {
        addbankname ()
    })
    
    //银行信息删除
    functio delefun1(deldeidnum) {
        document.getElementById("otherbankNum" + deldeidnum).remove();
    }
    function delefun2(deldeidnum) {
        document.getElementById("otherbankName" + deldeidnum).remove();
    }

    传值:最重要的是要获取到动态添加的div,通过 document.getElementsByClassName这个方法获取到相关的input元素,返回的是一个数组,然后遍历数组获取值

    //银行卡开户行信息
    var bankNameinputvalueArr = []
    function bankNameinputvalue() {
        var inputval = document.getElementsByClassName('otherbankNameinput')
        console.log(inputval)
        for (var i = 0; i < inputval.length; i++) {
            var bankNameinputvalue = {
                id: '',
                value:''
            }
            console.log(inputval[i]);
            console.log(inputval[i].value);
            if (inputval[i].value) {
                bankNameinputvalue.id = i
                bankNameinputvalue.value = inputval[i].value
            }
            bankNameinputvalueArr.push(bankNameinputvalue)
        }
        console.log(bankNameinputvalueArr)
    }
    //银行卡账号
    var bankNuminputvalueArr = []
    function bankNuminputvalue() {
        var inputval = document.getElementsByClassName('otherbankNuminput')
        console.log(inputval)
        for (var i = 0; i < inputval.length; i++) {
            var bankNuminputvalue = {
                id: '',
                value:''
            }
            console.log(inputval[i]);
            console.log(inputval[i].value);
            if (inputval[i].value) {
                bankNuminputvalue.id = i
                bankNuminputvalue.value = inputval[i].value
            }
            bankNuminputvalueArr.push(bankNuminputvalue)
        }
        console.log(bankNuminputvalueArr)
    }

    根据缓存值在页面生成div,并将值显示在input框,通过 document.querySelectorAll 获取生成的所有input,然后循环遍历把值显示在对应的input框。

    if(personalbaInfo.bankNameinputvalueArr){
                for(var i = 0; i< personalbaInfo.bankNameinputvalueArr.length-1; i++){
                    addbankname()
                }
                for(var j = 0; j < personalbaInfo.bankNameinputvalueArr.length; j++) {
                    document.querySelectorAll('.otherbankName input')[j].value = personalbaInfo.bankNameinputvalueArr[j].value
                }
            }
            if(personalbaInfo.bankNuminputvalueArr){
                for(var i = 0; i< personalbaInfo.bankNuminputvalueArr.length-1; i++){
                    addbanknum()
                }
                for(var j = 0; j < personalbaInfo.bankNuminputvalueArr.length; j++) {
                    document.querySelectorAll('.otherbankNum input')[j].value = personalbaInfo.bankNuminputvalueArr[j].value
                }
            }
    不积跬步无以至千里
  • 相关阅读:
    (华中科大)江南雨烟 C++ STL 专栏
    MoreWindows 微软认证专家博客目录(白话算法,C++ STL,windows编程)
    「转」基于Jmeter和Jenkins搭建性能测试框架
    【转】docker之Dockerfile实践
    Python 统一动态创建多个model对应的modelForm类(type()函数)
    owasp zap 安全审计工具 安装/拦截请求
    【转】持续集成 Sonar 平台搭建及 Sonar 自定义规则打包部署篇
    【转】SonarQube配置自定义的CheckStyle代码规则
    【转+整理】jenkins与SonarQube集成
    【转】jenkins插件pipeline使用介绍
  • 原文地址:https://www.cnblogs.com/lyt0207/p/12357612.html
Copyright © 2020-2023  润新知