title: set of selected value after ajax call
body: <p>I have a form in which two selects are correlated.
  After selecting a value from the first, an ajax call is triggered to recover the values of the second one. 
  I use this form for both insert and edit of the data.
  The first case is all smooth.
  The edit instead doesn't cooperate because when I try to set the selected value of the second select the operation doesn't show anything.
  Here's the code:</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;select class="inputtext" id="category" name="category" onchange="loadsub(); return false;"&gt;
      &lt;option value="0"&gt; -- &lt;/option&gt;
      &lt;?php code to load the values ?&gt;
  &lt;/select&gt;   
  &lt;br /&gt;  
  &lt;strong&gt;Subcategory&lt;/strong&gt;                
  &lt;select class="inputtext" id="subcategory" name="subcategory"&gt;
      &lt;option value="0"&gt; -- &lt;/option&gt;
  &lt;/select&gt;   
  </code></pre>
  
  <p>AJAX </p>
  
  <pre><code>function open(id, edit) {
      $('#prod').css("display", "block");
      request= 2;
      $.ajax({
          type: "POST",
          dataType: "json",
          url: "ajax/prod.php",
          data: {
              id: id,
              richiesta: request
          },
          async: true,
          success : function(data) {
              var i = 0;
              var tmp = '';
  
              if (data) {
                  while(i&lt;data.length) {
                      $('#category option[value="'+data[0]['categoria']+'"]').attr('selected', 'selected');
                      loadsub();
                      subcat= data[0]['subcategory'];
                      if (edit == 1) {
                          $('#categoria').attr("disabled", true);
                          $('#sottocategoria').attr("disabled", true);
                      }
                      i++;
                  }               
              }
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) { 
              //alert("Status: " + textStatus);
              //alert("Error: " + errorThrown); 
          },
          complete: function () {
              $('#subcategory option[value="' + subcat + '"]').attr('selected', 'selected');
          }
      });
  }
  </code></pre>