views:

95

answers:

2

I have two combobox and one url which is sending me json data to populate inside the combobox. How can I use same url to populate the combobox.

 `Can I use` $(".loctype").autocomplete({

source: function(request, response){

$.ajax..
}

});

I am not sure whether this is possible or not. I will appreciate if someone point me toward right direction.

my html code :

<p>
            <label for="ltype">Location Type</label>
            <select id="loctype">
            <option value="1">Warehouse</option>
            <option value="2">New Burg</option>

            </select>
        </p>
        <p>
            <label for="sku">Pallet Status</label>
            <select id="pstatus">
            <option value="3">3</option>
            <option value="4">4</option>

            </select>
        </p>
+1  A: 

Not sure if I understand your exact question, but if you return multiple fields:

return Json(new { Result = "success", location = whatever.location_id, status = whatever.pstatus });

You can assign:

if (data.Result == "success") {
      $('#loctype').val(data.location);
      $('#pstatus').val(data.status); 
}

EDIT I assumed you were sending back the selected value. To add an option to the dropdown, you would need to return the text and value.

$('#loctype').append($('<option></option>').val(data.location_id).html(data.location_name));

If you also want that new value to be selected, append .attr("selected", true)

RememberME
+1  A: 

You don't need Jquery to populate a combobox. See this.

After getting json data, you can iterate through it and populate your comboboxes. Let's say that your data is like the following:

data = {'loctype':["Warehouse", "New Burg"], 'pstatus':[3,4]};

Now let's iterate through it and populate lists:

for(var i=0; i < data.loctype.length; i++) { // loctype and pstatus arrays have same length
    addOption(data.loctype[i], data.loctype[i], "loctype");
    addOption(data.pstatus[i], data.pstatus[i], "pstatus ");

}

The method, addOption is as follows:

function addOption(text,value,cmbId) {
  var newOption = new Option(text, value);
  var lst = document.getElementById(cmbId);
  if (lst) lst.options[lst.options.length] = newOption;
}
Zafer