這是我到目前爲止。請閱讀代碼中的評論。它包含我的問題。如何顯示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"}
。我最終想要的是,如果我通過關鍵3
,我想得到Stanley Furniture
回來,如果我通過Stanley Furniture
,我得到了3
回來。由於3
是customerId,Stanley Furniture
是我的數據庫中的customerName。
什麼? JSON是JavaScript對象的字符串表示形式,因此如果您想以常規對象的形式訪問它,它*就會*需要解析... – James 2010-03-03 17:44:06
'$ .getJSON'已經這樣做了。 – BalusC 2010-03-03 17:45:12
啊,好點。 – James 2010-03-03 17:51:52