如果其中的输入符合某个条件,我试图隐藏在html表中的tr。 标准由下拉列表的选定值定义。 我是这样做的:
$(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页面内变得非常慢,但由于它太重而无法完成。 关于如何加快速度的任何建议?
答案 0 :(得分:1)
绩效优化的关键点
onchange
)额外奖励:学会使用else if
因为你的分支变得更加清晰。
以下是代码:
$(function () {
// pre-select things that won't change
var $sel = $('#p_Selection');
var tableRow = $('.goods .detail-child tr');
//
// ONCHANGE EVENT
// keep in mind that everything in it will be very costly
//
$sel.live('change', function () {
var type = $sel.attr('value');
tableRow.each(function (index) {
// this just makes the code shorter
var record = '#Record'+index;
// simple ID selector is enough, since its unique
var Record_LidExpected = $(record+'_LidExpected').attr('value');
var Record_LidObtained = $(record+'_LidObtained').attr('value');
var Record_QuantityExpected = $(record+'_QuantityExpected').attr('value');
var Record_QuantityObtained = $(record+'_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();
}
});
});
});
答案 1 :(得分:0)
在我的改写中你会注意到一些事情。
$('body').find()
因为它没有比直接选择#p_Selection
更有优势。input
函数选择$()
值时,我添加this
作为第二个参数。这基本上告诉jQuery在this
中查找输入(在这种情况下是指每个循环中的当前tr
。这里的优点是jQuery不需要搜索整个DOM该特定输入,仅在当前tr
标记内。switch
语句和一些||
运算符清理了if / else逻辑。我认为现在应该跑得更快。
<script type="text/javascript">
$(function() {
$('#p_Selection').live('change', function() {
var type = $(this).val();
var Record_LidExpected,
Record_LidObtained,
Record_QuantityExpected,
Record_QuantityObtained;
$('.goods .detail-child tr').each(function(index) {
Record_LidExpected = $('input[id$=Record['+index+']_LidExpected]', this).val();
Record_LidObtained = $('input[id$=Record[' + index + ']_LidObtained]', this).val();
Record_QuantityExpected = $('input[id$=Record[' + index + ']_QuantityExpected]', this).val();
Record_QuantityObtained = $('input[id$=Record[' + index + ']_QuantityObtained]', this).val();
switch(type) {
case "1" :
if (Record_LidExpected != Record_LidObtained || Record_QuantityExpected != Record_QuantityObtained) {
$(this).hide();
}
break;
case "2" :
if (Record_LidExpected == Record_LidObtained || Record_QuantityExpected == Record_QuantityObtained) {
$(this).hide();
}
break;
case "0":
$(this).show();
break;
}
});
});
});
</script>