1

如何使用下面提到的 jQuery 函数 检索petKeys和值?employeeKey

var whenSelectDateFromCalendar = function () {
    initKeyValues();
    petKeys = ? employeeKey = ?
};


var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
    var employeeKey = $('#employee input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
}
4

2 回答 2

4

您可以在一个对象中返回这两个值。尝试这个:

var whenSelectDateFromCalendar = function () {
    var keys = initKeyValues();

    // use the object returned above to set the vars here
    petKeys = keys.petKeys;
    employeeKey = keys.employeeKey;
};


var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
    var employeeKey = $('#employee input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    // return the object containing both values
    return { 
        employeeKey: employeeKey,
        petKeys: petKeys
    }
}
于 2013-09-10T10:18:51.643 回答
1

选项 1 - 在一个对象中返回多个结果

var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    var employeeKey = $('#employee input:checked').map(function () { return $(this).val(); }).get().join('+');}

    return {"petKeys": petKeys,  "employeeKey": employeeKey}
};

var whenSelectDateFromCalendar = function () {
    var result = initKeyValues();

    petKeys = result.petKeys;
    employeeKey = result.employeeKey;   

    // or even...

    petKeys = result["petKeys"];
    employeeKey = result["employeeKey"];
};

选项 2:在数组中返回多个结果

var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    var employeeKey = $('#employee input:checked').map(function () { return $(this).val(); }).get().join('+');}

    return [petKeys, employeeKey];
};

var whenSelectDateFromCalendar = function () {
    var result = initKeyValues();

    petKeys = result[0];
    employeeKey = result[1];
};
于 2013-09-10T10:26:27.143 回答