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?
The key points of preformance optimization
onchange)Bonus: learn to use
else ifbecause your branches become clearer.Here is the code: