在动态添加表数据的同时检查html表中的重复项

时间:2014-05-13 10:50:56

标签: jquery html-table

我的表看起来像这样:

<table style="width:100%" class="table table-striped table-bordered" id="xxxxxx">
    <thead>
        <tr>
            <th>Item Id</th>
            <th>Item Name</th>
            <th>Rate</th>
            <th>Quantity</th>
            <th>Total</th>
            <th>Edit</th>
        </tr> 
    </thead>
</table>

我在按钮点击时将数据添加到我的表格中:

var item_x = $('#itemIdx').val();
var item_x_name = $('#itemx option:selected').val();
var rate_x = $('#ratex').val();
var quantity_x = $('#quantityx').val();
var total_x = rate_x * quantity_x;

var add_to_table = '<tr><td>' + item_x + '</td><td>' + item_x_name + '</td><td>' + rate_x + '</td><td>' + quantity_x + '</td><td>' + total_x + "</td><td><buttonDelete</button></td>";
$('#xxxxxx').append(add_to_table);

现在,在动态添加数据时,我希望根据item_x值复制数据,无论它是否重复。如果重复,则显示警报;如果不是简单地添加到xxxxxx到表格。

1 个答案:

答案 0 :(得分:2)

您可以使用以下方式检查存在:

var tdlength= $("td").filter(function() {
    return $(this).text().toLowerCase() == item_x ;//get td with item_x 
 }).length;

if(!tdlength){ //td with html item_x  does not exists.
   //code here
}