세 개의 객체를 만들었습니다. 각 객체는 서로 다른 배열을 가지고 있습니다.자바 스크립트 - 개체를 어떻게 호출합니까?
var redmeat = {options: ["beef", "steak", "kangaroo"]},
whitemeat = {options: ["chicken", "turkey"]},
fish = {options: ["salmon", "tuna"]};
그런 다음 HTML에서 세 가지 해당 옵션이있는 선택 필드를 만들었습니다.
<select name="meat-type">
<option value="redmeat">Red meat</option>
<option value="whitemeat">White meat</option>
<option value="fish">Fish</option>
</select>
이제 사용자가 필드를 선택할 때마다 내 콘솔에 올바른 배열을 표시 할 수 있기를 원합니다. (예를 들어, 붉은 고기를 선택하면, 콘솔은 "쇠고기", "스테이크", "캥거루"를 기록합니다.)
$('select[name=meat-type]').change(function(){
var e = $("select[name=meat-type] option:selected").val();
function updatefield (e){
console.log(e); //this works, easy.
f = e.options; //but this doesn't work, return as undefined
console.log(f);
};
updatefield(e);
});
그래서 질문입니다; 어떻게 객체를 호출할까요?
문구 "call the object"는별로 의미가 없습니다. – Pointy
'e'는 정의한 객체가 아니라 문자열입니다. –
예 'e'는 문자열이지만 문제는; 그 문자열을 객체로 바꿀 수있는 방법이 있습니까, 그렇다면 e.options를 할 수 있을까요? – user3540018