0

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());
});
4

3 Answers 3

0

AJAX is asynchronous, and therefore the setting of the out variable from within it will happen after the rest of the function.

      $.ajax({
        type    : "POST",
        cache    : false,
        url      : "./pokus_zad.php",
        data    : JSON.stringify(pole2),
        success  : function(data) {
          out = JSON.parse(data);  //data.id
          $(this).closest('table').find('select').each(
            function() {
              $(this).append($('<option>', { "value" : out.id }).text(nazev));
            });
          $(this).closest('select').val(out.id).attr('selected', 'selected');
        }
      });
Sign up to request clarification or add additional context in comments.

Comments

0

You should put the append code inside the success block. Because the ajax call is asynchronous the append statement runs before the 'out' variable is set by the ajax callback.

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
          $(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());
});

Comments

0

Great, thanks guys!

Based on your suggestions I moved my append code to success callback of AJAX function AND I also had to create variable for $(this) since it is different $(this) inside AJAX than it is outside. ;)

The working JQuery part now looks like this:

var out = {};
$('select').bind("change", function() {
    if ($(this).val() == "new") {
        var nazev = prompt('Zadejte název zadavatele', $(this).parent().siblings(':first-child').text());
        if (nazev == null) {
            $(this).val($.data(this, 'current'));
            return false;
        } else {
            var select = $(this);
            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
                    select.closest('table').find('select').each(
                        function() {
                            $(this).append($('<option>', { "value" : out.id }).text(nazev));
                        });
                    select.val(out.id).attr('selected', 'selected');
                }
            });
        }
    }
    $.data(this, 'current', $(this).val());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.