Javascript HTML DOM表删除/删除选定行

Javascript HTML DOM表删除/删除选定行,javascript,jquery,html,dom,html-table,Javascript,Jquery,Html,Dom,Html Table,我有以下代码。 我正在尝试包括一个删除按钮来删除所选的一行或多行。。。在代码中包括选择和删除函数,但我不知道如何获取所选行并将其传递给函数以删除它。。。 一个包含在代码中的按钮应该足够了,但是如果你知道如何在每一行包含一个删除按钮“X”,那么请告诉我 以下是示例页面: 以下是一些其他代码,但与我的代码不兼容: 测试代码 var pricelist=新映射({Name:1,Preis:2}); 价格表。套(“气球”,25); var ddamount=新映射({Name:1,amount:2}

我有以下代码。 我正在尝试包括一个删除按钮来删除所选的一行或多行。。。在代码中包括选择和删除函数,但我不知道如何获取所选行并将其传递给函数以删除它。。。 一个包含在代码中的按钮应该足够了,但是如果你知道如何在每一行包含一个删除按钮“X”,那么请告诉我

以下是示例页面: 以下是一些其他代码,但与我的代码不兼容:


测试代码
var pricelist=新映射({Name:1,Preis:2});
价格表。套(“气球”,25);
var ddamount=新映射({Name:1,amount:2});
ddamount.set(“气球”,1);
.产品{
列表样式:无;
右边距:300px;
填充:0px;
身高:100%;
}
李先生{
显示:内联;
浮动:左;
利润率:10px;
}
.项目{
显示:块;
文字装饰:无;
}
.项目img{
边框:1px实心#333;
}
.项目p{
保证金:0;
字体大小:粗体;
文本对齐:居中;
颜色:#C3C3;
}
.购物车{
位置:固定;
右:0;
排名:0;
宽度:300px;
身高:100%;
背景:#ccc;
填充:0px 10px;
}
h1{
文本对齐:居中;
颜色:#555;
}
氢{
位置:绝对位置;
字体大小:16px;
左:10px;
底部:20px;
颜色:#555;
}
.总数{
保证金:0;
文本对齐:右对齐;
右边填充:20px;
}
th{
边框:1px#DDD实心;
填充物:5px;
游标:指针;}
.选定{
背景颜色:棕色;
颜色:#FFF;
}
变量数据={“总计”:0,“行”:[]};
var总成本=0;
变量名;
风险价值金额;
var价格;
//拖降函数
$(函数(){
$('#cart')。数据网格({
singleSelect:true//在这里使用false,我可以选择多于1个
});
$('.item')。可拖动({
回复:对,
代理:'clone',
onStartDrag:function(){
$(this).draggable('options')。游标='不允许';
$(this).draggable('proxy').css('z-index',10);
},
onStopDrag:function(){
$(this).draggable('options').cursor='move';
}
});
$('.cart')。可拖放({
onDragEnter:函数(e,源){
$(源).draggable('options').cursor='auto';
},
onDragLeave:函数(e,源){
$(source).draggable('options')。cursor='not-allowed';
},
onDrop:函数(e,源){
name=$(source.find('p:eq(0)').html();
addProduct(名称,parseFloat(ddamount.get(名称));//parseFloat(price.split('€')[1])
}
});
});
函数addProduct(名称、金额){
金额=浮动(金额);
price=pricelist.get(名称);
价格=金额*价格;
函数add(){
对于(var i=0;i
document.getElementById('btn1')。addEventListener('click',函数(事件){
addProduct(“Balloon”,document.getElementById(“balloamount”).innerHTML);
});


运货马车
名称
量
价格
X
document.getElementById('resetbtn')。addEventListener('click',函数(事件){
如果(确认(“确定?”)==真){
$('#cart').datagrid('loadData',{“total”:0,“rows”:[]});
总成本=0;
$('div.cart.total').html('total:0'+0);
}
});
总计:0


我会这样做:

首先,我们向html表中的每一行添加一个带有删除按钮的
td

然后我们可以将一个侦听器附加到
#table
元素(带有
id=“table”
的元素)。这个侦听器侦听具有类
delete table的
#table
的子体上的单击,当单击一个时,我们点击按钮并找到离按钮最近的
tr`我们把它移除


$(“#表”)。在('单击','上。删除行',函数(){
$(this).closest('tr').remove();
//$(这)是按钮
});
td{border:1px#DDD-solid;padding:5px;cursor:pointer;}
.选定{
背景颜色:棕色;
颜色:#FFF;
}

1法拉利F138
1 000€
1 200€
2013年经济转型前的莫诺波斯图-斯库德里亚法拉利车型
1.
F138
克里克预详细
删除
2法拉利F138
1 000€
1 200€
2013年经济转型前的莫诺波斯图-斯库德里亚法拉利车型
1.
F138
克里克预详细
删除
3法拉利F138
1 000€
1 200€
2013年经济转型前的莫诺波斯图-斯库德里亚法拉利车型
1.
F138
克里克预详细
删除

以下是我过去做过类似事情的一个例子

基本上
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Test code</title>
    <link rel="stylesheet" type="text/css" href="http://www.w3cschool.cc/try/jeasyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="http://www.w3cschool.cc/try/jeasyui/themes/icon.css">

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script type="text/javascript" src="http://www.w3cschool.cc/try/jeasyui/jquery.easyui.min.js"></script>

    <script type="text/javascript">
    var pricelist = new Map({Name: 1, Preis: 2});
        pricelist.set("Balloon", 25);
    var ddamount = new Map({Name: 1, amount: 2});
        ddamount.set("Balloon", 1);
    </script>

    <style type="text/css">
        .products{
            list-style:none;
            margin-right:300px;
            padding:0px;
            height:100%;
        }
        .products li{
            display:inline;
            float:left;
            margin:10px;
        }
        .item{
            display:block;
            text-decoration:none;
        }
        .item img{
            border:1px solid #333;
        }
        .item p{
            margin:0;
            font-weight:bold;
            text-align:center;
            color:#c3c3c3;
        }
        .cart{
            position:fixed;
            right:0;
            top:0;
            width:300px;
            height:100%;
            background:#ccc;
            padding:0px 10px;
        }
        h1{
            text-align:center;
            color:#555;
        }
        h2{
            position:absolute;
            font-size:16px;
            left:10px;
            bottom:20px;
            color:#555;
        }
        .total{
            margin:0;
            text-align:right;
            padding-right:20px;
        }
        th {
            border: 1px #DDD solid; 
            padding: 5px; 
            cursor: pointer;}
        .selected {
            background-color: brown;
            color: #FFF;
        }
    </style>
<!------------------------------------------------------------------------------------------------------- -->
    <script>
        var data = {"total":0,"rows":[]};
        var totalCost = 0;
        var name;
        var amount;
        var price;

//DragDropFunction
        $(function(){
            $('#cart').datagrid({
                singleSelect:true //here with false i can select more then 1
            });
            $('.item').draggable({
                revert:true,
                proxy:'clone',
                onStartDrag:function(){
                    $(this).draggable('options').cursor = 'not-allowed';
                    $(this).draggable('proxy').css('z-index',10);
                },
                onStopDrag:function(){
                    $(this).draggable('options').cursor='move';
                }
            });
            $('.cart').droppable({
                onDragEnter:function(e,source){
                    $(source).draggable('options').cursor='auto';
                },
                onDragLeave:function(e,source){
                    $(source).draggable('options').cursor='not-allowed';
                },
                onDrop:function(e,source){
                    name = $(source).find('p:eq(0)').html();
                    addProduct(name, parseFloat(ddamount.get(name))); //parseFloat(price.split('€')[1])
                }
            });
        });

        function addProduct(name, amount){
            amount = parseFloat(amount);
            price = pricelist.get(name);
            price = amount*price;
            function add(){
                for(var i=0; i<data.total; i++){
                    var row = data.rows[i];
                    if (row.name == name){
                        row.quantity += amount;
                        row.price += price;
                        return;
                    }
                }
                data.total += 1;
                data.rows.push({
                    name:name,
                    quantity:amount, //statt amount
                    price:price
                });
            }
            add();
            totalCost += price;
            $('#cart').datagrid('loadData', data);
            $('div.cart .total').html('Total: €'+totalCost);
        }

//HERE---------------------------------------------------------------------------------------------------------

        function deleteRow(r) {
            var i = r.parentNode.parentNode.rowIndex;
            document.getElementById("cart").deleteRow(i); //instead of i i want the selected row/or rows.. i can select more thn one
    </script>
</head>
<!-- ---------------------------------------------------------------------------------------------------- -->

<body style="margin:0;padding:0;height:100%;background:#fafafa;">
    <ul class="products">
        <li>
            <a href="#" class="item"> <!-- class ist Pflicht -->
                <div>
                <img src="images/shirt1.gif" style="height="300"; width="150";"/>
                </div>
                <div>
                    <p style="font-size:14pt">Balloon</p>
                    <p>Price: €25</p>
                    <p style="font-size:12pt">Amount:</p>
                </div>
            </a>
            <div>
            <textarea type="amount" id="Balloonamount" rows="1" cols="6" style="overflow:hidden"><?php echo $amount;?></textarea>
            <input align="right" type="button" value="ADD" id="btn1" /> <!-- style="width:30px" --> 
            <script>
            document.getElementById('btn1').addEventListener('click', function(event) {
                addProduct("Balloon", document.getElementById('Balloonamount').innerHTML);
            });
            </script>
            </div>
        </li>
    </ul>
<!-- ---------------------------------------------------------------------------------------------------- -->
    <div class="cart">
        <h1>Cart</h1>
        <div style="background:#fff">
        <table id="cart" fitColumns="true" style="width:300px;height:auto;">
            <thead>
                <tr>
                    <th field="name" width=140>Name</th>
                    <th field="quantity" width=60 align="right">Quantity</th>
                    <th field="einheit" width=20 align="right"></th>
                    <th field="price" width=60 align="right">Price</th>
                    <th field="delete" width=20 align="right">X</th> <!--here the delete button -->
                </tr>
            </thead>
        </table>
        </div>
        <div align="center">
        <input type="button" value="Delete" width=auto align="center" id="deletebtn" onclick="deleteProduct()">
        <input type="button" value="Reset" width=auto align="right" id="resetbtn" onclick="">
            <script>
            document.getElementById('resetbtn').addEventListener('click', function(event) {
                if (confirm("Sure?") == true) {
                $('#cart').datagrid('loadData', {"total":0,"rows":[]});
                totalCost = 0;
                $('div.cart .total').html('Total: €'+0);
                }
            });
            </script>
        </div>
        <p class="total"><b>Total: €0</b></p>
    </div>
</body>
</html>
// ADD A DELETE BUTTON ON EACH ROW
$('td').each(function(i, val){ 
    $('td').eq(i).prepend('<span class="deleteButton">Delete</span>')
});
// WHEN DELETE BUTTON IS CLICKED REMOVE THE TABLE ROW
$(document).on('mousedown', '.deleteButton', function(){
     $('tr').eq($('.deleteButton').index(this)).remove();
})