如何使用jquery

时间:2017-01-21 14:05:53

标签: jquery html5 bootstrapping

我有一个表格,我可以动态添加行。我有每行的复选框。我想删除所选的行。

我的html页面如下

                <table class="table">
                <thead>
                <tr>
                    <th></th>
                    <th>Loads</th>
                    <th>Quantity</th>
                    <th>Watts</th>
                    <th>Hours/day</th>
                    <th>Watt Hrs/day</th>
                <tr>
                </thead>
                    <tbody>
                <tr>
                    <td><input type="checkbox"></td>                        
                    <td>Fans</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>                    
                    <td>Tubelights</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>
                </tbody>
            </table>
            <form id="myform">
                <input type="text" id="name" placeholder="Name" required="true"/>
                <button type="submit" class="add-row">Add Row</button>
                <button id="remove" type="button">Reomve Selected</button>
            </form>

我的脚本如下

<script>ole="form"
    $(document).ready(function(){
        $(".nav-tabs a").click(function(){
        $(this).tab('show');
        });
    });
    $(document).ready(function(){
        $(":text").each(function() {
            $(this).keyup(function(){
                findTotals();
            });
        });
    });
    function findTotals() {
        $("tbody tr").each(function() {
            row_total = 1; 
            $("input:text",this).each(function() {
                if(!isNaN(parseFloat($(this).val()))){
                row_total *= parseFloat($(this).val());
                }
            }); 
            if(row_total == 1){
                row_total = 0;
            }
            $("#sum",this).html((row_total/24).toFixed(2));
        });
    }
    $(document).ready(function(){
        $(".add-row").click(function(){
            var name = $("#name").val();
            if(name){
                var markup = "<tr><td><input type='checkbox'></td><td>"+ name +"</td><td><input type='text' class='form-control'></td><td><input type='text' class='form-control'></td><td><input type='text' class='form-control'></td><td><span id='sum'>0</span></td></tr>";
                $("table tbody").append(markup);
            }
            $('#myform').find('input:text').val(''); 
        });


    });
</script>

任何人都可以帮我这样做。

1 个答案:

答案 0 :(得分:1)

看看这个 单击删除按钮您只需要执行此操作

$("input[type=checkbox]:checked").closest("tr").remove();

&#13;
&#13;
$('#remove').click( function(){
		$("input[type=checkbox]:checked").closest("tr").remove();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
                <thead>
                <tr>
                    <th></th>
                    <th>Loads</th>
                    <th>Quantity</th>
                    <th>Watts</th>
                    <th>Hours/day</th>
                    <th>Watt Hrs/day</th>
                <tr>
                </thead>
                    <tbody>
                <tr>
                    <td><input type="checkbox"></td>                        
                    <td>Fans</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>                    
                    <td>Tubelights</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>                        
                    <td>Fans</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>                    
                    <td>Tubelights</td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><input type="text" class="form-control"></td>
                    <td><span id="sum">0</span></td>
                </tr>                
                </tbody>
            </table>
            <form id="myform">
                <input type="text" id="name" placeholder="Name" required="true"/>
                <button type="submit" class="add-row">Add Row</button>
                <button id="remove" type="button">Reomve Selected</button>
            </form>
&#13;
&#13;
&#13;