javascript
  • jquery
  • debugging
  • 2011-07-13 16 views -2 likes 
    -2

    このコードで何が問題になっていますか?私はjQueryを使用しています。このコードで何が問題になっていますか?

    function setPreviewUrl() { 
         $post_title = $("#post_title").val(); 
         $post_content = $("#post_content").val(); 
         $.get('ajax/base64encode.php?name='+post_title, function(data1) { 
          var $postTitle = null; 
          $postTitle = data1; 
         }); 
         $.get('ajax/base64encode.php?name='+post_content, function(data2) { 
          var $postCont = null; 
          $postCont = data2; 
         }); 
         var $urlExten = null; 
         $urlExten = '?post_title='+postTitle+'&post_cont='+postCont; 
         $("a[href*='#change']").attr('href', '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
         $("#preview_frame").prop("href", '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
        } 
    
    +0

    あなたはそれが何を期待し、あなたがそれを実行したときに何が起こるか何? – interjay

    +0

    問題は何ですか? – rahul

    +0

    おそらくあなたが達成したいものを書いてください –

    答えて

    1

    あなたは非同期コードをめちゃくちゃにしているように思える:

    function setPreviewUrl() { 
         var $post_title = null; 
         $post_title = $("#post_title").val(); 
         var $post_content = null; 
         $post_content = $("#post_content").val(); 
         $.get('ajax/base64encode.php?name='+post_title, function(data1) { 
          var $postTitle = null; 
          $postTitle = data1; 
         }); 
         $.get('ajax/base64encode.php?name='+post_content, function(data2) { 
          var $postCont = null; 
          $postCont = data2; 
         }); 
         // Those functions above will execute when the get is done, in the 
         // meantime the code below is executed, so the variables have not been 
         // set yet. 
         // Also, the variables are only declared inside the get function so they 
         // are not accessible due to their scope. 
         var $urlExten = null; 
         $urlExten = '?post_title='+postTitle+'&post_cont='+postCont; 
         $("a[href*='#change']").attr('href', '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
         $("#preview_frame").prop("href", '<?php echo $site_url; ?>admin/ajax/preview.php'+urlExten); 
        } 
    
    関連する問題