如何使用下面提到的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('+');
}
答案 0 :(得分: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
}
}
答案 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"];
};
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];
};