jQuery:检查列是否包含特定值
我有一个包含各种值的标准表。 相同的值可能出现在不同的TD中,但不在同一列中。
例:
text1 text2 text3 text4 text5 text6
有没有办法我可以使用jQuery来检查某个列是否包含某个值,如果是,则返回1,否则返回0?
示例:将此应用于上面的第二列并检查“ text2
”时,它应返回1; 如果我检查“ text5
”它也应该返回1; 如果我检查“ text6
”,它应返回0,因为此列中不存在。
我在考虑开始这样的事情:
$('#myTable td:nth-child(2)').each(function() { //... });
蒂姆,谢谢你对此有任何帮助。
像这样的东西
$.fn.colCheck = function(col, text) { var c = Array.isArray(col) ? col : [col], t = this, a = []; $.each(c, function(_, v){ a.push( t.find('tr td').filter(function() { return $(this).index() === (v-1) && $.trim($(this).text()) === text; }).length ); }); return a.length === 1 ? a[0] : a; }
小提琴
用得像
$('#myTable').colCheck([1,2,3], 'text5'); // pass array, return array [0,1,0] $('#myTable').colCheck(2, 'text2'); // 1
将返回给定列中该文本的出现次数。
像这样:
text1 text2 text3 text4 text5 text6 //more TRs...
和jquery:
function check() { var flag = 0; $('#myTable td.check').each(function(){ if($(this).html() == '') { flag = 0; return flag; } else { flag = 1; } }) return flag; }
function mycheck(what) { var result = 0; $( "#myTable td" ).each(function( index ) { var myval = $( this ).text(); if ((index - 1) % 3 == 0 ) { if (myval == what) { result = 1; return result; } } }); return result; }