将2个javascript函数合并为1

时间:2014-08-14 11:23:34

标签: javascript jquery

我继承了一些旧代码,并且我正在尝试清理它,我在网站上使用jQuery,因此使用它可以保持简洁和简单。

function address_finder_callback() {
    address_finder_setter('addressPostCode', 'addressLine1', 'addressLine2', 'addressLine3', 'addressLine4');
}

function address_finder_setter(obj0, obj1, obj2, obj3, obj4) {
    var formElement;
    var buttonClicked = document.getElementById('addressFinderPanelForm_buttonClicked').value;

    if (buttonClicked) {
        formElement = 'addressFinderPanelForm_postcode'
        var hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj0).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line0';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj1).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line1';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj2).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line2';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj3).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line3';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj4).value = hiddenField;
        hiddenField = "";
    }
}

我想出来并使用jQuery来整理它 -

答案

function address_finder_callback() {

    var postCode = $('#addressFinderPanelForm_postcode').val();
    var adressLine1 = $('#addressFinderPanelForm_line0').val();
    var addressLine2 = $('#addressFinderPanelForm_line1').val();
    var addressLine3 = $('#addressFinderPanelForm_line2').val();
    var addressLine4 = $('#addressFinderPanelForm_line3').val();

    $("#addressPostCode").val(postCode);
    $("#addressLine1").val(adressLine1);
    $("#addressLine2").val(addressLine2);
    $("#addressLine3").val(addressLine3);
    $("#addressLine4").val(addressLine4);

}

1 个答案:

答案 0 :(得分:0)

那么第一个功能似乎毫无意义?

function address_finder_callback(obj0, obj1, obj2, obj3, obj4) {
    var formElement;
    var buttonClicked = document.getElementById('addressFinderPanelForm_buttonClicked').value;

    if (buttonClicked) {
        formElement = 'addressFinderPanelForm_postcode'
        var hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj0).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line0';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj1).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line1';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj2).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line2';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj3).value = hiddenField;
        hiddenField = "";

        formElement = 'addressFinderPanelForm_line3';
        hiddenField = document.getElementById(formElement).value;
        document.getElementById(obj4).value = hiddenField;
        hiddenField = "";
    }
}

直接打电话

可以简化为:

    function address_finder_callback(obj0, obj1, obj2, obj3, obj4) {
            var formElement;
            var buttonClicked = document.getElementById('addressFinderPanelForm_buttonClicked').value;

            if (buttonClicked) {
document.getElementById(obj0).value = document.getElementById(addressFinderPanelForm_postcode).value;
document.getElementById(obj1).value = document.getElementById(addressFinderPanelForm_line0).value; document.getElementById(obj2).value = document.getElementById(addressFinderPanelForm_line1).value;
document.getElementById(obj3).value = document.getElementById(addressFinderPanelForm_line2).value;
document.getElementById(obj4).value = document.getElementById(addressFinderPanelForm_line3).value;
            }
        }

甚至更多,如果你使用jquery并循环它,但你得到漂移