trigger Onload - how to?
trigger Onload - how to?
Hello everybody,
I am pretty new to jQuery and I could need some help on a rather complex problem.
My goal is to populate selectboxes in a chain with the help of php, mysql and jQuery. E.g: Germany, Bayern, München
I found some great code which I have adapted to make it suite my neads. It basically works, but I have the following problem. Right now the code only works if one selects for example the province. Then the script returns the cities. I would also need to make the script pull the cities upon load of the page in some cases. The purpose is to preselect the city if a user has already made a selection on another html page.
Something like onload or so!? Here is the code I am using:
-
(function ($) {
$.fn.selectChain = function (options) {
var defaults = {
key: "id",
value: "label"
};
var settings = $.extend({}, defaults, options);
if (!(settings.target instanceof $)) settings.target = $(settings.target);
return this.each(function () {
var $$ = $(this);
$$.change(function () {
var data = null;
if (typeof settings.data == 'string') {
data = settings.data + '&' + this.name + '=' + $$.val();
} else if (typeof settings.data == 'object') {
data = settings.data;
data[this.name] = $$.val();
}
settings.target.empty();
$.ajax({
url: settings.url,
data: data,
type: (settings.type || 'get'),
dataType: 'json',
success: function (j) {
settings.target.slideDown();
//if (settings.target == $('#elementSelect'))
$('#quarter').slideDown();
var options = [], i = 0, o = null;
for (i = 0; i < j.length; i++) {
// required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
o = document.createElement("OPTION");
o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
settings.target.get(0).options[i] = o;
}
// hand control back to browser for a moment
setTimeout(function () {
settings.target
.find('option:first')
.attr('selected', 'selected')
.parent('select')
.trigger('change');
}, 0);
},
error: function (xhr, desc, er) {
// add whatever debug you want here.
//alert("an error occurred "+er);
//settings.target.slideToggle();
}
});
});
});
};
})(jQuery);
$(function () {
var cat = $('#categorySelect');
var el = $('#elementSelect');
var attr = $('#attributeSelect');
el.selectChain({
target: attr,
url: '/subapp_inserate/select-chain-data.php',
data: { ajax: true, anotherval: "anotherAction" }
})
//.trigger('change');
// note that we're assigning in reverse order
// to allow the chaining change trigger to work
cat.selectChain({
target: el,
url: '/subapp_inserate/select-chain-data.php',
data: { ajax: true }
}).trigger('work');
});
Thank you for any help!