jQuery: The Write Less, Do More JavaScript Library

Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

View: New views
5 Messages — Rating Filter:   Alert me  

Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

by Tan-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, all.
I have two listbox:
a listbox country
<select id="edit-field-country-value" class="form-select" name="country
[value]">
<option selected="selected" value="">  - None -</option>
<option value="1">Malaysia</option>
<option value="2">Thailan</option>
</select>
a listbox city
<select id="edit-field-city-value" class="form-select" name="field_city
[value]">
<option selected="selected" value="">  - None -</option>
</select>

When i select country then i use ajax response data { "1": "Quan 1",
"2": "Quan 2", "3": "Quan 3", "4": "Quan 4" }
1, 2, 3,4 is key of array
I want when select country then add key and values  of Json in listbox
city
Please help me

Re: Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

by Thai Dang Vu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you try this (never try it by myself)

$('#edit-field-city-value > option:not(first-child)').remove();
for (var key in your_json_object) {
    $('#edit-field-city-value').append('<option value="' + key + '">' + your_json_object[key] + '</option>');
}

On Wed, Nov 4, 2009 at 4:06 AM, Tan <it_qn2004@...> wrote:
Hi, all.
I have two listbox:
a listbox country
<select id="edit-field-country-value" class="form-select" name="country
[value]">
<option selected="selected" value="">  - None -</option>
<option value="1">Malaysia</option>
<option value="2">Thailan</option>
</select>
a listbox city
<select id="edit-field-city-value" class="form-select" name="field_city
[value]">
<option selected="selected" value="">  - None -</option>
</select>

When i select country then i use ajax response data { "1": "Quan 1",
"2": "Quan 2", "3": "Quan 3", "4": "Quan 4" }
1, 2, 3,4 is key of array
I want when select country then add key and values  of Json in listbox
city
Please help me


Re: Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

by MorningZ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

var drp = document.getElementById("edit-field-city-value");
drp.options.length == 1; // Clear all existing values except first
$.each(AjaxData, function(v, k) {
       drp.options[drp.options.length] = new Option(k, v);
});

Re: Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

by Tan-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Dang Vu. It's work.

On Nov 4, 7:41 pm, Thai Dang Vu <tdan...@...> wrote:

> Can you try this (never try it by myself)
>
> $('#edit-field-city-value > option:not(first-child)').remove();
> for (var key in your_json_object) {
>     $('#edit-field-city-value').append('<option value="' + key + '">' +
> your_json_object[key] + '</option>');
>
> }
> On Wed, Nov 4, 2009 at 4:06 AM, Tan <it_qn2...@...> wrote:
> > Hi, all.
> > I have two listbox:
> > a listbox country
> > <select id="edit-field-country-value" class="form-select" name="country
> > [value]">
> > <option selected="selected" value="">  - None -</option>
> > <option value="1">Malaysia</option>
> > <option value="2">Thailan</option>
> > </select>
> > a listbox city
> > <select id="edit-field-city-value" class="form-select" name="field_city
> > [value]">
> > <option selected="selected" value="">  - None -</option>
> > </select>
>
> > When i select country then i use ajax response data { "1": "Quan 1",
> > "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" }
> > 1, 2, 3,4 is key of array
> > I want when select country then add key and values  of Json in listbox
> > city
> > Please help me

Re: Fill key va value from response of Json { "1": "Quan 1", "2": "Quan 2", "3": "Quan 3", "4": "Quan 4" } in listbox.

by Tan-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks MorningZ.

On Nov 4, 9:21 pm, MorningZ <morni...@...> wrote:
> var drp = document.getElementById("edit-field-city-value");
> drp.options.length == 1; // Clear all existing values except first
> $.each(AjaxData, function(v, k) {
>        drp.options[drp.options.length] = new Option(k, v);
>
> });