irrelevant, it's more than fast enough. You can improve code
readability and performance (a bit) using the Option constructor:
$.post('/path/to/json.php', {params: 'xxx'}, function(data){
var $select = $('#mySelectBox');
$.each(data, function(){
$select.append( new Option(this.State.name, this.State.name) );
});
}, 'json');
On Jun 26, 2:56 pm, Alexandre Magno <alexan...@gmail.com> wrote:
> Hello,
>
> Yeah, thats right, the code works but it's too heavy
>
> You should concatenate one string with all option and then make a
> append. A append inside the loop would break the perfomance:
>
> > $.post('/path/to/json.php', {params: 'go here, if you need them'},
> > function(data){
> > // "data" contains that JSON object you described
>
> > // Also, I'm assuming your select box looks something like this:
> > // <select id="mySelectBox"></select>
> > var select = $('#mySelectBox');
> > var options;
> > if (data.length) $.each(data, function(){
> > options += '<option value="'+this.State.name+'">'+this.State.name+'</option>';
> > })
> > $(options).appendTo( select );
>
> > }, 'json');
>
> Reference:http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-cor...
>
> Regards,
>
> Alexandre Magno
> Interface Developerhttp://blog.alexandremagno.net
>
> On Jun 26, 11:59 am, Eric Garside <gars...@gmail.com> wrote:
>
> > Well, lets assume you're getting the JSON you described from the
> > following ajax call:
>
> > $.post('/path/to/json.php', {params: 'go here, if you need them'},
> > function(data){
> > // "data" contains that JSON object you described
>
> > // Also, I'm assuming your select box looks something like this:
> > // <select id="mySelectBox"></select>
> > var select = $('#mySelectBox');
> > if (data.length) $.each(data, function(){
> > $( '<option value="' + this.State.name + '">' + this.State.name
> > + '</option>').appendTo( select );
> > })
>
> > }, 'json');
>
> > On Jun 26, 6:46 am, liaogz82 <liaog...@gmail.com> wrote:
>
> > > Hi all,
>
> > > I have this JSON file that does the encoding from the states database.
> > > I want to extract it out using jquery and populate the select drop
> > > down box. the json file is being encoded in this way:
>
> > > [
> > > {"State":{"name":"AUSTRALIA CAPITAL TERRITORY"}},
> > > {"State":{"name":"NEW SOUTH WALES"}},
> > > {"State":{"name":"NORTHERN TERRITORY"}},
> > > {"State":{"name":"QUEENSLAND"}},
> > > {"State":{"name":"SOUTH AUSTRALIA"}},
> > > {"State":{"name":"TASMANIA"}},
> > > {"State":{"name":"VICTORIA"}},
> > > {"State":{"name":"Western Australia"}}
> > > ]
>
> > > not very sure how to extract out the values. Please help me. Thanks!
No comments:
Post a Comment