여기까지 왔어. 코드의 주석을 읽으십시오. 내 질문이 들어 있습니다.JSON 객체의 값을 어떻게 표시합니까?
var customer; //global variable
function getCustomerOption(ddId){
$.getJSON("http://localhost:8080/WebApps/DDListJASON?dd="+ddId, function(opts) {
$('>option', dd).remove(); // Remove all the previous option of the drop down
if(opts){
customer = jQuery.parseJSON(opts); //Attempt to parse the JSON Object.
}
});
}
function getFacilityOption(){
//How do I display the value of "customer" here. If I use alert(customer), I got null
}
다음은 내 json 개체의 모양입니다 : {"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}
. 내가 궁극적으로 원하는 건, 만약 내가 키를 넘겨 준다면, 나는 Stanley Furniture
을 되찾고 싶고, 내가 Stanley Furniture
을 넘겨 준다면, 3
이 돌아왔다. 3
은 customerId이고 Stanley Furniture
은 내 데이터베이스의 customerName입니다.
무엇? JSON은 JavaScript 객체의 문자열 표현이므로 일반 객체로 액세스하려는 경우 구문 분석이 필요합니다. – James
'$ .getJSON'은 이미 그렇게합니다. – BalusC
아, 좋은 지적이야. – James