替代JS / JQuery中的许多if / else语句?

时间:2013-07-17 20:50:55

标签: javascript jquery performance if-statement

我正在尝试将许多页面元素从其初始值与用户更改的内容进行比较,以提示他们保存等。

在没有很多if / else语句的情况下,有没有更好的方法来进行此检查?交换机不起作用,因为有许多不同的值被检查,而不是一个有很多情况的单个值。欢迎任何和所有提示/指示,谢谢!

if ($('#InitialhidLeadType').val() != $('#hidLeadType').val())
        bIsChange = true;
    else if ($('#InitialhidProductType').val() != $('#hidProductType').val())
        bIsChange = true;
    else if ($('#InitialhidFixedFilterType').val() != $('#hidFixedFilterType').val())
        bIsChange = true;
    else if ($('#InitialhidMinCreditScore').val() != $('#hidMinCreditScore').val())
        bIsChange = true;
    else if ($('#InitialhidMaxCreditScore').val() != $('#hidMaxCreditScore').val())
        bIsChange = true;
    else if ($('#InitialhidMinLoanAmount').val() != $('#hidMinLoanAmount').val())
        bIsChange = true;

    else if ($('#InitialhidMinLTV').val() != $('#hidMinLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMaxLTV').val() != $('#hidMaxLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMinCLTV').val() != $('#hidMinCLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMaxCLTV').val() != $('#hidMaxCLTV').val())
        bIsChange = true;
    else if ($('#InitialhidPropertyType').val() != $('#hidPropertyType').val())
        bIsChange = true;
    else if ($('#InitialhidPropertyUse').val() != $('#hidPropertyUse').val())
        bIsChange = true;
    else if ($('#InitialhidBankruptcy').val() != $('#hidBankruptcy').val())
        bIsChange = true;
    else if ($('#InitialhidForeclosure').val() != $('#hidForeclosure').val())
        bIsChange = true;
    else if ($('#InitialhidLoanPurpose').val() != $('#hidLoanPurpose').val())
        bIsChange = true;
    else if ($('#InitialhidIsCashout').val() != $.trim($('#hidIsCashout').val()))
        bIsChange = true;
    else if ($('#InitialhidNoCreditScore').val() != $.trim($('#hidNoCreditScore').val()))
        bIsChange = true;
    else if ($('#InitialhidRelationship').val() != $.trim($('#hidRelationship').val()))
        bIsChange = true;
    else if ($('#InitialhidCurrentLoanVA').val() != $.trim($('#hidCurrentLoanVA').val()))
        bIsChange = true;
    else if ($('#InitialhidFoundHome').val() != $.trim($('#hidFoundHome').val()))
        bIsChange = true;
    else if ($('#InitialhidFHA').val() != $.trim($('#hidFHA').val()))
        bIsChange = true;
    else if ($('#InitialhidIsConforming').val() != $.trim($('#hidIsConforming').val()))
        bIsChange = true;
    else if ($('#InitialhidMSA').val() != $('#hidMSA').val())
        bIsChange = true;
    else if ($('#InitialhidStatedCreditRating').val() != $('#hidStatedCreditRating').val())
        bIsChange = true;
    else if ($('#InitialhidCampEffectivedate').val() != $('#hidCampEffectivedate').val())
        bIsChange = true;
    else if ($('#InitialhidCampExpirationdate').val() != $('#hidCampExpirationdate').val())
        bIsChange = true;
    else if ($('#InitialhidIsFixedFilter').val() != $('#hidIsFixedFilter').val())
        bIsChange = true;
    else if ($('#InitialhidTestCompaign').val() != $('#hidTestCompaign').val())
        bIsChange = true;

    else if ($('#InitialhidSelectedStates').val() != $('#hidSelectedStates').val())
        bIsChange = true;
    else if ($('#InitialhidVolumeTieredPricing').val() != $('#hidVolumeTieredPricing').val())
        bIsChange = true;
    else if ($('#InitialhidWeekDayCapacity').val() != $('#filterCapacity').val())
        bIsChange = true;
    else if ($('#InitialhidWeekendCapacity').val() != $('#filterCapacityWeekEnd').val())
        bIsChange = true;
    else if ($('#InitialhidHolidayCapacity').val() != $('#filterCapacityHoliday').val())
        bIsChange = true;

///////////////////////////////////// HTML Below //////////////////////////////////////

<div id = "changeCheck">

<asp:HiddenField ID="InitialhidLeadType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidProductType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFixedFilterType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinLoanAmount" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxLoanAmount" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinCLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxCLTV" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidWeekDayCapacity" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidWeekEndCapacity" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidHolidayCapacity" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidPropertyType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidPropertyUse" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidBankruptcy" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidForeclosure" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidLoanPurpose" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsCashout" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidNoCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidRelationship" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidCurrentLoanVA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFoundHome" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFHA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsConforming" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMSA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidStatedCreditRating" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidCampEffectivedate" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidCampExpirationdate" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsFixedFilter" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidTestCompaign" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidSelectedStates" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidVolumeTieredPricing" ClientIDMode="Static" runat="server" />

5 个答案:

答案 0 :(得分:9)

迭代元素,比较每个元素,如果有变化,则提示用户保存:

var save = false;

$('[id^="Initial"]').each(function() {
    if ( this.value != $('#'+this.id.replace('Initial','')).val()) save = true;
});

if (save) confirm(' would you like to save ');

通常你会以不同的方式解决这个问题,方法是在更改元素时更改变量,而不是在某个时候检查所有元素:

var save = false;

$(':input').on('change', function() {
    save = true;
});

window.onbeforeunload = function() {
    if (save) confirm(' would you like to save ');
}

答案 1 :(得分:0)

改变你的方法。而不是检查所有值的更改,截取表单的change事件,以便在输入更改时设置标志:

$('#some-form-element').on('change', function (e) {
    // ... set flag here
});

答案 2 :(得分:0)

这会告诉您是否有任何表单元素的值在任何时候都发生了变化。

var formIsDirty = false;
$(document).ready(function() {
    $(':input').change(function() {
        formIsDirty = true;
    });
});

当然,如果他们将值更改回原始值,则不会告诉您。如果这还不够,你可以这样做:

$(document).ready(function() {
    $(':input').each(function() {
        $(this).data('original', $(this).val());
    });
});

然后当您验证时:

    var formIsDirty = false;
    $(':input').each(function() {
        if ($(this).data('original') != $(this).val()) {
            formIsDirty = true;
            return false;
        }
    });

第二个选项可以让您取消隐藏的“初始值”字段。

答案 3 :(得分:0)

试试这个:

var map = [
    ['#InitialhidLeadType', '#hidLeadType'],
    ['#InitialhidProductType', '#hidProductType'],
    ['#InitialhidFixedFilterType', '#hidProductType']
    // etc..
];
for (var i = 0; i < map.length; i++) {
    if ($(map[i][0]).val() != $(map[i][1]).val()) {
        bIsChange = true;
        break;
    }
}

答案 4 :(得分:0)

我想说的是adeneo和ACEfanatic02建议的两种方法的组合。 。 。扭曲:

$("input:visible").change(function() {
    var currID = $(this).prop("id");
    var initialValContainer = $("#Initial" + currID);

    if (initialValContainer.length > 0) {
        var currVal = $.trim($(this).val());
        var initialVal = $.trim(initialValContainer.val());

        if (currVal === initialVal) {
            $(this).addClass("changed");
        }
        else {
            $(this).removeClass("changed");
        }
    }
});

此时,只要您准备好检查是否要保存,就可以使用以下代码来查看是否有必要:

if ($(".changed").length > 0) {
    **prompt to save**
}

这样,代码只会在至少有一个输入被标记为已更改时才会提示。此方法还允许您“取消”用户返回其原始值的输入,以及如果需要使用$(".changed").removeClass("changed")“重置”所有输入(例如,如果您保存新值而不页面重新加载)。