我有通过循环数据生成的表,如下所示:
$.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()
。
答案 0 :(得分:0)
你可以用
检查jQuery中的类名$("selector").attr("class")
应该返回元素的类名
答案 1 :(得分:0)
这是一些类似的代码模拟,一个函数显示如何在添加之前检查html,另一个函数检查表中是否存在重复项(你似乎在做什么)
var array_rule_segments = ["widget1type,widgetdetails1,widgetorder1,widget1message", "widget1type,widgetdetails1,widgetorder1,widget1message"];
function rowExistsBeforeAdd(row) {
var newRow = $(row);
var cell;
var foundsome;
var details = newRow.find('.widget_details').text();
console.log(details);
var result = $('#summary_table tr').find('td:contains(' + details + ')');
console.log("result:" + result.length);
// details match if true (has length) in incomming row
if (result.length > 0) {
// filter to see if other things match in the new row with the inputs
foundsome = result.filter(function () {
var mytype = $(this).find(".widget_type").text();
var myorder = $(this).find(".widget_order").text();
console.log("Type:" + mytype);
console.log("order:" + myorder);
console.log(myorder == $('#widget_order').val());
console.log(mytype == $('#widget_types').val());
return myorder == $('#widget_order').val() && mytype == $('#widget_types').val();
});
if (foundsome.length) {
console.log('found' + foundsome.length);
}
}
return foundsome.length;
}
function checkRow() {
var cell;
var foundsome;
var details = $('#input_widget_details').val();
console.log(details);
var result = $('#summary_table tr').find('td:contains(' + details + ')');
console.log("result:" + result.length);
if (result.length > 0) {
foundsome = result.parent().siblings().filter(function () {
var mytype = $(this).find(".widget_type").text();
var myorder = $(this).find(".widget_order").text();
console.log("Type:" + mytype);
console.log("order:" + myorder);
console.log(myorder == $('#widget_order').val());
console.log(mytype == $('#widget_types').val());
return myorder == $('#widget_order').val() && mytype == $('#widget_types').val();
});
if (foundsome.length) {
console.log('found' + foundsome.length);
}
}
return foundsome.length;
}
function addrow() {
$.each(array_rule_segments, function (key, listofwidgets) {
//console.log('in outer loop');
alert(listofwidgets.length);
var widget_details = listofwidgets.split(',');
var counter = key + 1;
var htmlstring = '';
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>';
if (!rowExistsBeforeAdd(htmlstring)) {
$(htmlstring).appendTo('#summary_table');
}
/* too late, already added!
if (checkRow()) {
alert('duplicates');
}
*/
});
}
$("#add_to_table").on("click", function () {
addrow();
});
虚假加价:
<table id="summary_table"></table>
<button id="add_to_table">add</button>
<br/>
<input type="text" id="widget_types" value="widget1type" />
<input type="text" id="widget_order" value="widgetorder1" />
<input type="text" id="input_widget_details" value="widgetdetails1" />
小提琴让你可以看到控制台日志等工作:http://jsfiddle.net/QTkpw/