[英]jQuery hiding all trs or tds not just one
I am trying to compare a date from an input field to the ones displayed in my table. 我想比较输入字段中的日期和表格中显示的日期。 However, when matched, it hides all tds and erases the column, if I use the .parent() it hides all trs when matches so I have an empty table.
但是,当匹配时,它会隐藏所有tds并删除列,如果我使用.parent()它会在匹配时隐藏所有trs,所以我有一个空表。
jQuery : jQuery:
$('[name="dateFrom"]').on("change", function () {
var dateFrom = new Date($(this).val());
var conditionDateFrom = $('td[name="tdDateFrom');
conditionDateFrom.hide();
conditionDateFrom.filter(function () {
return dateFrom < new Date(conditionDateFrom.html());
}).show();
});
input: 输入:
<input name="dateFrom" type="date" value="2016-01-01" />
the td I am trying to match 我试图匹配的td
<td name="tdDateFrom">{{ a.StartDate | date:'longDate'}}</td>
I just want to hide the one tr where it matches the td. 我只想隐藏与td匹配的一个tr。
Thanks 谢谢
Cannot get a jsfiddle to work but here is a screen of the console + table 无法让jsfiddle工作但这里是控制台+表的屏幕
The table: http://imgur.com/BWecBdn 表: http : //imgur.com/BWecBdn
The console when logging http://imgur.com/bMwGDpd 记录http://imgur.com/bMwGDpd时的控制台
var test = new Date(conditionDateFrom.html());
console.log(test);
Only seems to be checking the condition of the first tr? 似乎只是检查第一个tr的条件?
JSFiddle https://jsfiddle.net/jp3jf25a/ similar to my code, however for me the table is being affected, cannot get it to work on the fiddle JSFiddle https://jsfiddle.net/jp3jf25a/类似于我的代码,但是对我来说桌子受到影响,无法让它在小提琴上工作
Various issues: 各种问题:
conditionDateFrom.html()
you are hiding all of the tds, instead you should use $(this).text()
. conditionDateFrom.html()
您隐藏了所有tds,而应使用$(this).text()
。 <=
not <
because an equal date should show <=
not <
因为应显示相同的日期 Updated code: 更新的代码:
$('[name="dateFrom"]').on("change", function () {
var conditionDateFrom = $('.table tr');
if($(this).val() == ''){
conditionDateFrom.show();
return;
}
var dateFrom = new Date($(this).val());
conditionDateFrom.hide();
conditionDateFrom.filter(function (index) {
return index == 0 || dateFrom <= new Date($(this).children('td[name="tdDateFrom"]').text());
}).show();
});
let's breakdown your problem, you want to hide only the first match i guess But instead you are fetching all the matches. 让我们分解你的问题,你想只隐藏我猜的第一场比赛但是你取而代之的是所有的比赛。
$('td[name="tdDateFrom')
will fetch you all matches instead of that you can get only first match by $('td[name="tdDateFrom').first()
. $('td[name="tdDateFrom')
将获取所有匹配,而不是只能通过$('td[name="tdDateFrom').first()
获得第一个匹配$('td[name="tdDateFrom').first()
。
`$('td[name="tdDateFrom').first().parent('tr')` will return you only the one matched tr element.
here is updated fiddle https://jsfiddle.net/jp3jf25a/1/ 这里是更新的小提琴https://jsfiddle.net/jp3jf25a/1/
I think this should work to select the appropriate tr. 我认为这应该可以选择合适的tr。
var inputValue = $(input).attr('value') //'2016-01-01'
var conditionDateFrom = $("td:contains(inputValue)").parent()
http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/
edited after realizing you want the parent tr (I think?) 在意识到你想要父母tr之后编辑(我想?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.