我有通过循环数据生成的表,如下所示:
$.each(array_rule_segments, function (key, listofwidgets) {
//console.log('in outer loop');
var widget_details = listofwidgets.split(',');
var counter = key + 1
htmlstring += '<tr id="listofwidgets_' + key + '">';
htmlstring += '<td><input type="button" value="Remove" class="remove"/></td>';
htmlstring += '<td name="widget_type" class="widget_type" id="widget_type' + counter + '">' + widget_details[0] + '</td>';
htmlstring += '<td name="widget_details" class="widget_details" id="widget_details' + counter + '">' + widget_details[1] + '</td>';
htmlstring += '<td name="widget_order" class="widget_order" id="widget_order' + counter + '">' + widget_details[2] + '</td>';
htmlstring += '<td name="messages" id="messages' + counter + '">' + widget_details[3] + '</td>';
htmlstring += '</tr>';
});
然后稍后,我有逻辑来检查用户尝试添加到表中的新小部件是否已经存在。例如,假设我在表中已有一个小部件,widget_details 字段中的值为“1234”。
我这样检查:
$("#add_to_table").live("click", function () {
var cell;
var result = $('#summary_table tr').find('td:contains(' + $('#input_widget_details').val() + ')'); //find cell in table with same widget dteails information...
if (result.length > 0) {
//check for duplicates using class names.
console.log(result.siblings(".widget_order").html());
if (result.siblings(".widget_order").html() == $('#widget_order').val() && result.siblings(".widget_type").html() == $('#widget_types').val()) {
alert("Duplicate rule segment!");
return false;
}
}
第一个 if 语句有效,它找到具有匹配的小部件详细信息值的行,但是,我无法根据它们的类名检查同级单元格。我尝试检查兄弟节点上的 html() 的调试语句产生的值为 null。
我也试过.val()
代替.html()
.