sarode111 sarode111 -4 years ago 108
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.


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 Source

First you need to split your data:


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 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download