I have this code http://jsfiddle.net/meridius/ysDGm/ where the problem is with out.id
variable in append
part. For reasons I can't explain, that variable is empty in that part.
I think the problem is with that variable's scope and I tried everything but with no luck. Please note, the problem is NOT with AJAX, that works fine!
Here is shortened version of that Fiddle:
<tr>
<td>one</td>
<td>
<select name="zadavatel">
<option value="0">-- vyberte --</option>
<option value="new">-- nový zadavatel --</option>
</select>
</td>
</tr>
var out = {};
$('select').bind("change", function() {
if ($(this).val() == "new") {
var nazev = prompt('Question',
$(this).parent().siblings(':first-child').text());
if (nazev == null) {
$(this).val($.data(this, 'current'));
return false;
} else {
var pole2 = {};
pole2["nazev"] = nazev;
$.ajax({
type : "POST",
cache : false,
url : "./pokus_zad.php",
data : JSON.stringify(pole2),
success : function(data) {
out = JSON.parse(data); //data.id
}
});
out.id = 15; // this work, but it SHOULD work without it (with AJAX)
$(this).closest('table').find('select').each(
function() {
$(this).append($('<option>', { "value" : out.id }).text(nazev));
});
$(this).closest('select').val(out.id).attr('selected', 'selected');
}
}
$.data(this, 'current', $(this).val());
});