新增删除input效果

在这里插入图片描述

新增删除input效果

 <div class="form-group">
            <label class="col-sm-3 control-label">临床诊断2</label>
            <div class="col-sm-3" id="test">
                <input  name="diagnosis" class="form-control" id="diagnosis"
                          ></input>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"></label>
            <div class="col-sm-4">
                <div class="form-group">
                    <div class="col-sm-8">
                        <a class="btn btn-primary" onclick="addInput()">
                            <i class="fa fa-plus"></i> 新增
                        </a>
                        <a class="btn btn-danger" onclick="deleteInput()">
                            <i class="fa fa-remove"></i> 删除
                        </a>
                    </div>
                </div>
            </div>
        </div>
 var diagnosisList = [[${diagnosisList}]];
    if (diagnosisList==null){
        diagnosisList="";
    }
    var diagno = diagnosisList.split(',');
    var str="";
    for (var i=0;i<diagno.length;i++){

        if (i==0){
            // $("#diagnosis").value(diagno[i])
            $("#diagnosis").attr("value", diagno[i])
        }
        else{
            str+='<input  name="diagnosis" class="form-control" style="margin-top: 2%" id="'+'diagnosis'+(i)+'"\n' +
                ' value="'+diagno[i]+'"></input>'

            // $("#diagnosis"+(i+1)).val(diagno[i])
        }
    }
    $("#test").append(str);
    var detail_div = 0;
    var element = $("input[name='diagnosis']");
    for (var i=0;i<element.length;i++){
        console.log(element[i].id)

        detail_div=element.length;
    }

    function addInput() {
        var e = document.getElementById("diagnosis");
        var div = document.createElement("input");
        div.className = "form-control";
        div.setAttribute('style','margin-top:2%');
        div.id = "diagnosis" + detail_div;
        div.name = "diagnosis";
        div.innerHTML = e.innerHTML;
        document.getElementById("test").appendChild(div);
        detail_div++;
    }

    function deleteInput() {
        if (detail_div ==1) {
            layer.msg('至少保留一项', {icon: 0})
            return;
        }
        if(detail_div >0){
            var id = "diagnosis" + (detail_div - 1).toString();
            var e = document.getElementById(id);
            document.getElementById("test").removeChild(e);
            detail_div--;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是使用jQuery完成购物车效果的示例代码,您可以根据自己的需求进行修改: HTML代码: ``` <table id="product-table"> <thead> <tr> <th>商品名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td>商品1</td> <td>10.00</td> <td><input type="number" class="quantity" value="1"></td> <td class="subtotal">10.00</td> <td><button class="remove">删除</button></td> </tr> <tr> <td>商品2</td> <td>20.00</td> <td><input type="number" class="quantity" value="1"></td> <td class="subtotal">20.00</td> <td><button class="remove">删除</button></td> </tr> </tbody> </table> <div id="total-price">总价:<span>30.00</span></div> <button id="clear-cart">清空购物车</button> ``` JavaScript代码: ``` $(document).ready(function() { // 计算总价 function calculateTotal() { var total = 0; $('.subtotal').each(function() { total += parseFloat($(this).text()); }); $('#total-price span').text(total.toFixed(2)); } // 更新小计 function updateSubtotal(row) { var price = parseFloat(row.find('td:eq(1)').text()); var quantity = parseInt(row.find('.quantity').val()); var subtotal = price * quantity; row.find('.subtotal').text(subtotal.toFixed(2)); } // 初始化小计和总价 $('tbody tr').each(function() { updateSubtotal($(this)); }); calculateTotal(); // 修改数量时更新小计和总价 $('.quantity').on('input', function() { var row = $(this).closest('tr'); updateSubtotal(row); calculateTotal(); }); // 删除商品时更新总价 $('.remove').on('click', function() { $(this).closest('tr').remove(); calculateTotal(); }); // 清空购物车 $('#clear-cart').on('click', function() { $('tbody').empty(); calculateTotal(); }); }); ``` 这段代码实现了一个简单的购物车效果,包括添加、删除商品以及计算总价等功能。您可以根据自己的需求进行修改和扩展。 ### 回答2: 以下是使用jq完成购物车效果的代码示例: HTML部分: ```html <div> <h2>商品列表</h2> <ul id="product-list"> <li> <span class="product-name">商品1</span> <span class="product-price">10元</span> <img class="product-image" src="product1.jpg" alt="商品1图片"> <button class="add-to-cart">加入购物车</button> </li> <li> <span class="product-name">商品2</span> <span class="product-price">20元</span> <img class="product-image" src="product2.jpg" alt="商品2图片"> <button class="add-to-cart">加入购物车</button> </li> <!-- 其他商品列表项 --> </ul> <h2>购物车</h2> <ul id="cart-list"></ul> <h2>添加新商品</h2> <input id="new-product-name" type="text" placeholder="商品名称"> <input id="new-product-price" type="text" placeholder="商品单价"> <button id="add-new-product">添加</button> </div> ``` JavaScript部分: ```javascript $(document).ready(function() { // 加入购物车按钮点击事件 $('.add-to-cart').click(function() { var productName = $(this).siblings('.product-name').text(); var productPrice = $(this).siblings('.product-price').text(); var productImage = $(this).siblings('.product-image').attr('src'); var cartItem = $('<li><span class="cart-item-name">' + productName + '</span><span class="cart-item-price">' + productPrice + '</span><img class="cart-item-image" src="' + productImage + '" alt="' + productName + '图片"><button class="remove-from-cart">移除</button></li>'); $('#cart-list').append(cartItem); }); // 从购物车中移除按钮点击事件 $('#cart-list').on('click', '.remove-from-cart', function() { $(this).parent().remove(); }); // 添加新商品按钮点击事件 $('#add-new-product').click(function() { var newProductName = $('#new-product-name').val(); var newProductPrice = $('#new-product-price').val(); var newProductItem = $('<li><span class="product-name">' + newProductName + '</span><span class="product-price">' + newProductPrice + '</span><button class="add-to-cart">加入购物车</button></li>'); $('#product-list').append(newProductItem); $('#new-product-name').val(''); $('#new-product-price').val(''); }); }); ``` 以上代码实现了一个简单的购物车效果。点击"加入购物车"按钮会将对应商品添加到购物车列表中,点击"移除"按钮会从购物车中移除该商品,点击"添加"按钮可以通过输入框的内容来生成新的商品。请注意,该示例代码只是简单示范,实际使用中可能需要根据具体需求进行适当的修改和完善。 ### 回答3: 以下是使用jq完成购物车效果的示例代码: HTML代码: ```html <!DOCTYPE html> <html> <head> <title>购物车</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> </head> <body> <h1>购物车</h1> <div id="product-list"> <div class="product"> <img src="product1.jpg"> <h2>商品1</h2> <p class="price">100元</p> <button class="add-btn">添加至购物车</button> <button class="remove-btn">从购物车移除</button> </div> <div class="product"> <img src="product2.jpg"> <h2>商品2</h2> <p class="price">200元</p> <button class="add-btn">添加至购物车</button> <button class="remove-btn">从购物车移除</button> </div> </div> <h2>新增商品</h2> <input type="text" id="product-name" placeholder="商品名称"> <input type="text" id="product-price" placeholder="商品价格"> <button id="add-product-btn">添加商品</button> <h2>购物车</h2> <ul id="cart"></ul> <script> $(document).ready(function(){ // 添加商品至购物车 $(".add-btn").click(function(){ var product = $(this).parent(); var name = product.find("h2").text(); var price = product.find(".price").text(); $("#cart").append("<li>" + name + " - " + price + "</li>"); }); // 从购物车移除商品 $(".remove-btn").click(function(){ $(this).parent().remove(); }); // 添加新商品 $("#add-product-btn").click(function(){ var name = $("#product-name").val(); var price = $("#product-price").val(); if(name && price){ $("#product-list").append( `<div class="product"> <h2>${name}</h2> <p class="price">${price}元</p> <button class="add-btn">添加至购物车</button> <button class="remove-btn">从购物车移除</button> </div>` ); // 清空输入框 $("#product-name").val(""); $("#product-price").val(""); // 重新绑定事件 bindEventHandlers(); } }); // 绑定按钮事件 function bindEventHandlers(){ $(".add-btn").click(function(){ var product = $(this).parent(); var name = product.find("h2").text(); var price = product.find(".price").text(); $("#cart").append("<li>" + name + " - " + price + "</li>"); }); $(".remove-btn").click(function(){ $(this).parent().remove(); }); } bindEventHandlers(); }); </script> </body> </html> ``` 以上代码演示了一个基本的购物车效果。通过点击"添加至购物车"按钮,可以将商品添加至购物车,通过点击"从购物车移除"按钮,可以将商品从购物车移除。在页面底部,新增了一个输入框和一个按钮,可以用于提交新的商品。扩展商品只需在输入框中输入商品名称和价格,点击"添加商品"按钮即可生成新的商品并显示在商品列表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值