开发者

getjson() jquery dropdown not working

I have the following json file:

{
"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"}
 }

I am tryin to populate the dropdown list in my js file using 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开发者_StackOverflow社区>';   
        }
            $('body #tabs #tabs-2 client').append  (this.display_name).append(INPUT_DROPDOWN).append(html).append(NEWLINE);         
    }
         });    

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

but I am not able to view the dropdown list and the values...Kindly point out where I am going wrong..

I want to populate like this VIEWPORT: dropdownlist values FRAMERATE:dropdownlist values PATTERN_RESOLUTION:dropdownlist values


Try

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

istead of .append(INPUT_DROPDOWN).append(html)

options must be in the select but you insert in the same level than the select. see http://jsfiddle.net/q7fWt/


Try adding the parameter "jsoncallback=?" to your URL

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

jQuery will substitute the last questionmark, after the jsoncallback parameter, with an ID.

This ID will then be used on server side to create a response that will start with a function named from the ID value.

That would result in a response that would look something like this:

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"}
});

So in a short answer, if your json response is NOT wrapped in this function name the callback function will not fire, instead you will get an error which you could see this way:

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

    //your code

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

Hope this helps

Patrik

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜