the following code works fine for me to show various sql-results after change a select-field:
$(document).ready(function() {
$('#main_kat').change(getDropdownOptions);
});
function getDropdownOptions() {
var val = $(this).val();
$.post('/ajax/joe.php?qid=<? echo $row_job['sub_quali'];?>', { value : val}, populateDropdown, 'html');
$("#send").prop('disabled', false);
}
function populateDropdown(data) {
if (data != 'error') {
$('#subcat').html(data);
}
}
my problem: for a new insert everything is great because i have to select manualy, but if i use this code for my update form, i want to have opend my div #subcat allready.
i tried following at the beginning:
kat = $('#main_kat').val();
getDropdownOptions(kat);
kat is set (checked) via alert, but the function will not be fired... where is my mistake ?
Thanks
you can not just call the function because you are using it with this.
kat = $('#main_kat').val();
getDropdownOptions(kat);
function getDropdownOptions() {
var val = $(this).val();
change to:
getDropdownOptions();
function getDropdownOptions() {
var val = $(document.getElementById('main_kat')).val();
I'll recommend you to replace your getDropdownOptions function by this:
function getDropdownOptions(val) {
val = val || $('#main_kat').val();
$.post('/ajax/joe.php?qid=<? echo $row_job['sub_quali'];?>', { value : val}, populateDropdown, 'html');
$("#send").prop('disabled', false);
}
It will probably work fine if you ensure that the this
reference points to the correct object when you call the function. Instead of passing kat
as a parameter you could just call the getDropdownOptions
method with the following code:
getDropdownOptions.apply(kat);