Sam Collett put together a great jQuery plugin for select box manipulation. It allows for the easy adding, removing, sorting, selecting, and copying of options in a select box without the need of loops. I downloaded the original from his site,
http://www.texotela.co.uk/code/jquery/select/, and started getting it up and running. Except for the fact that you can not do a post request the ajaxAddOption method, everything works great with no modification.
I modified the ajaxAddOption method by adding a sixth parameter, method, which tells the ajaxAddOption, that you would rather do a post request for the
JSON data rather than the standard get request.
Downloads:
jquery.selectboxes.js version 2.2.2
$.fn.ajaxAddOption = function(url, params, select, fn, args, method)
{
if(typeof(url) != "string") return this;
if(typeof(params) != "object") params = {};
if(typeof(select) != "boolean") select = true;
this.each(
function()
{
var el = this;
if(method != 'post')
{
$.getJSON(url,
params,
function(r)
{
$(el).addOption(r, select);
if(typeof fn == "function")
{
if(typeof args == "object")
{
fn.call(el, args);
}
else
{
fn.call(el);
}
}
}
);
} else {
$.ajax({
type: 'POST',
url: url,
data: params,
dataType: "json",
success: function(r) {
$(el).addOption(r, select);
if(typeof fn == "function")
{
if(typeof args == "object")
{
fn.call(el, args);
}
else
{
fn.call(el);
}
}
}
});
}
}
);
return this;
};