sarode111 sarode111 - 27 days ago 3
Ajax Question

Retrieve the multiple values in dropdownlist

I have two column in database table one is country and other is port.At the time of insertion selecting the country from dropdown related port is loaded in listbox.
Then we selecting the multiple port for one country and insert in to database.

Eg. Country:-India Port:-AGARTALA,BAGDOGRA,BANGALORE,COIMBATORE


The table view is like this. Now i need to retrieve all ports which is inserted in table in dropdown list one by one.How to Solve...Thanks in Advance.

Answer

First you need to split your data:

var data = 'Country:-India Port:-AGARTALA,BAGDOGRA,BANGALORE,COIMBATORE';

var portStart = data.lastIndexOf('-')+1;

//create an array of items by removing the garbage in the beginning
//and splitting on the commas
var ports = data.substr(portStart).split(',');

Then use jquery's $.each() method to iterate over all the items in the array:

// ports -> the array from above
// function()...  the operation to perform on each item in the array
$.each(ports, function (index, value) {
    //insert an option element into #ports with the value/text attributes set as shown
    $('#ports').append($('<option/>', { 
        value: value,
        text : value 
    }));
});