2011-08-29 112 views
0

我有以下的JSON文件:的getJSON()jQuery的下拉菜單無法正常工作

{ 
"viewport_size": 
       {"display_name":"VIEWPORT SIZE:", 
       "name":"viewport_size", 
       "format":"number", 
       "type":"dropdown", 
       "dflt":"640 * 480", 
       "values":["800*600","1280*720","1920*1080"], 
       "isMandatory":"true"}, 
"framerate": 
       {"display_name":"FRAMERATE:", 
       "name":"fps", 
       "format":"number", 
       "type":"dropdown", 
       "dflt":"30", 
       "values":["45","60","90"], 
       "isMandatory":"true"}, 
"pattern_resolution": 
       { "display_name":"PATTERN RESOLUTION:", 
       "name":"resoln", 
       "format":"number", 
       "type":"dropdown", 
       "dflt":"8", 
       "values":["16","32","64"], 
       "isMandatory":"true"} 
} 

我試着來填充我的js文件中使用的getJSON()下拉列表

var INPUT_TEXT='<input type="text"/>'; 
    var INPUT_RADIO='<input type="radio"/>'; 
    var INPUT_CHECKBOX='<input type="checkbox"/>'; 
    var INPUT_DROPDOWN='<select id="items"></select>'; 
    var SUBMIT_BUTTON='<input type="button" value="SUBMIT"/>'; 
    var NEWLINE='<br></br>'; 

$.getJSON('json_input/client_settings_input.json',function(clientData) 
{ 
$.each(clientData,function(i,feild) 
{ 
    if(this.type=="dropdown") 
    { 
     var html = '';  
     var len = feild.values.length;  
     //alert('lenght is'+len); 
     for (var i = 0; i< len; i++){   
      //alert('inside for'); 
      html += '<option>'+ feild.values[i]+'</option>'; 
     } 
      $('body #tabs #tabs-2 client').append (this.display_name).append(INPUT_DROPDOWN).append(html).append(NEWLINE);   
    } 
     });  

$('body #tabs #tabs-2 #client').append(SUBMIT_BUTTON); 
}); 

但我無法查看下拉列表和值...請指出我要出錯的地方..

我想像這樣填充VIEWPORT:dropdownlist values FRAMERATE:dropdownlist值 PATTERN_RESOLUTION:下拉列表值

回答

0

嘗試的.append(INPUT_DROPDOWN).append(html)

選項

.append($(INPUT_DROPDOWN).html(html)) 

istead必須在選擇,但你比同級別的選擇插入。 請參閱http://jsfiddle.net/q7fWt/

0

嘗試添加參數「jsoncallback =?」到你的網址

$.getJSON('json_input/client_settings_input.json?jsoncallback=?',function(clientData) 

jQuery將替換jsoncallback參數後的最後一個問號和一個ID。

此ID將在服務器端用於創建一個響應,該響應將從一個以ID值命名的函數開始。

這將導致反應會是這個樣子:

jQuery16205149872086476535_1314088378455({ 
    "viewport_size": 
      {"display_name":"VIEWPORT SIZE:", 
      "name":"viewport_size", 
      "format":"number", 
      "type":"dropdown", 
      "dflt":"640 * 480", 
      "values":["800*600","1280*720","1920*1080"], 
      "isMandatory":"true"}, 
"framerate": 
      {"display_name":"FRAMERATE:", 
      "name":"fps", 
      "format":"number", 
      "type":"dropdown", 
      "dflt":"30", 
      "values":["45","60","90"], 
      "isMandatory":"true"}, 
"pattern_resolution": 
      { "display_name":"PATTERN RESOLUTION:", 
      "name":"resoln", 
      "format":"number", 
      "type":"dropdown", 
      "dflt":"8", 
      "values":["16","32","64"], 
      "isMandatory":"true"} 
}); 

因此,在一個簡短的回答,如果你的JSON響應不裹在這個函數命名回調函數將不火,而不是你會得到你可以看到這樣一個錯誤:

$.getJSON('json_input/client_settings_input.json?jsoncallback=?',function(clientData) { 

    //your code 

}).error(function(jqXHR, textStatus, errorThrown) { 
    alert("Error: " + textStatus + " errorThrown: " + errorThrown); 
}) 

希望這有助於

帕特里克