隐藏给定特定条件的行(慢)
如果 tr 中的输入符合特定条件,我会尝试将 tr 隐藏在 html 表中。 该标准由下拉列表的选定值定义。 我这样做是这样的:
$(function () {
$('body').find('#p_Selection').live('change', function () {
var type = $('body').find('#p_Selection').attr('value');
var tableRow = $('.goods').find('.detail-child tr');
tableRow.each(function (index) {
var Record_LidExpected = $('input[id$=Record[' + index + ']_LidExpected]').attr('value');
var Record_LidObtained = $('input[id$=Record[' + index + ']_LidObtained]').attr('value');
var Record_QuantityExpected = $('input[id$=Record[' + index + ']_QuantityExpected]').attr('value');
var Record_QuantityObtained = $('input[id$=Record[' + index + ']_QuantityObtained]').attr('value');
if (type == "1") {
if (Record_LidExpected != Record_LidObtained) {
$(this).hide();
}
else {
if (Record_QuantityExpected != Record_QuantityObtained) {
$(this).hide();
}
}
}
else {
if (type == "2") {
if (Record_LidExpected == Record_LidObtained) {
$(this).hide();
}
else {
if (Record_QuantityExpected == Record_QuantityObtained) {
$(this).hide();
}
}
}
else {
if (type == "0") {
$(this).show();
}
}
}
});
});
});
这个脚本在我的 aspx 页面中变得非常慢,并且它无法完成,因为它太重了。 关于如何使其更快的任何建议?
I'm trying to hide tr's within a html table if the inputs inside them match a certain criteria.
The criteria is defined by a dropdown's selected value.
I'm doing it like so:
$(function () {
$('body').find('#p_Selection').live('change', function () {
var type = $('body').find('#p_Selection').attr('value');
var tableRow = $('.goods').find('.detail-child tr');
tableRow.each(function (index) {
var Record_LidExpected = $('input[id$=Record[' + index + ']_LidExpected]').attr('value');
var Record_LidObtained = $('input[id$=Record[' + index + ']_LidObtained]').attr('value');
var Record_QuantityExpected = $('input[id$=Record[' + index + ']_QuantityExpected]').attr('value');
var Record_QuantityObtained = $('input[id$=Record[' + index + ']_QuantityObtained]').attr('value');
if (type == "1") {
if (Record_LidExpected != Record_LidObtained) {
$(this).hide();
}
else {
if (Record_QuantityExpected != Record_QuantityObtained) {
$(this).hide();
}
}
}
else {
if (type == "2") {
if (Record_LidExpected == Record_LidObtained) {
$(this).hide();
}
else {
if (Record_QuantityExpected == Record_QuantityObtained) {
$(this).hide();
}
}
}
else {
if (type == "0") {
$(this).show();
}
}
}
});
});
});
This script became extremely slow inside my aspx page and it just won't complete because it is too heavy.
Any suggestions on how to make it faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
性能优化的要点
onchange
)奖励:学习使用
else if
因为你的分支变得更加清晰。这是代码:
The key points of preformance optimization
onchange
)Bonus: learn to use
else if
because your branches become clearer.Here is the code:
在我的重写中,你会注意到一些事情。
$('body').find()
因为与直接选择#p_Selection
相比,它并没有给您带来任何优势。$()
函数选择input
值时,我添加this
作为第二个参数。这基本上告诉 jQuery 在this
中查找输入(在本例中指的是每个循环中的当前tr
)。这里的优点是 jQuery 不需要为了在整个 DOM 中搜索特定的输入,就在当前的tr
标记内,switch
语句和一些稍微清理了 if/else 逻辑。 >||
运算符现在应该运行得更快。
In my rewrite you'll notice a few things.
$('body').find()
because it doesn't give you any advantage over selecting#p_Selection
directly.input
values using the$()
function, I am addingthis
as a second argument. This basically tells jQuery to look for the input withinthis
(which in this case refers to the currenttr
in the each loop. The advantage here is that jQuery doesn't need to search the entire DOM for that particular input, just within the currenttr
tag.switch
statement and some||
operators.I think that this should run faster now.